javaScript 对象~js中一切皆对象

来源:互联网 发布:阿里云 手机归属地 编辑:程序博客网 时间:2024/06/06 06:45
charCodeAt()方法,可返回指定位置字符的Unicode编码。
indexOf()方法,可返回某个指定字符串值在字符串中首次出现的位置。
lastIndexOf()方法,可返回一个指定的字符串最后出现的位置,在一个字符串中的指定位置从后向前搜索。
subString()方法,用去提取指定字符串中指定两个下标之间的字符(含左不含右)。
replace(regexp,replacement),在字符串中用一些字符串替换另一些字符,或替换一个与正则表达式匹配的子串。
toFixed(),将number四舍五入为指定小数位数的数字。
push(),向数组末尾添加一个或多个元素,并返回新的长度。
reverse(),颠倒数组中元素的顺序(改变原数组,不产生新数组)。
sort(),对数组元素进行排序(改变原数组,不产生新数组)。
round(),把数字舍入为最接近的整数(0.5先上取整)。
toLocaleDateString(),根据当地时间将Date对象的日期部分转换为字符串,并返回结果。
toLocaleTimeString(),根据当地时间将Date对象的时间部分转换为字符串,并返回结果。
getFullYear(),返回一个表示年份的四位数数字。
getMounth(),返回表示的月份(值是0~11的整数)。
exec(),用于检索字符串的正则表达式的匹配。
lastIndex属性,用于规定下次匹配的起始位置。
match(),方法用于在字符串内检索指定的值,找到一个或多个正则表达式的匹配。该方法类似于indexOf()和lastIndexOf(),但他返回指定的值,而不是字符串的位置。在全局检索模式下,match()方法将进行全局检索,找到所有匹配的子字符串。非全局检索模式下,只进行一次匹配。
search(),返回第一个匹配的子字符串的起始位置。

代码:
<!doctype html><html><head><meta charset="utf-8"/><title>内置对象</title><script>//www.w3school.com.cnconsole.log("1.String-----");var s = "JavaScript";console.log(s.charCodeAt(2));console.log(s.indexOf("a"));console.log(s.lastIndexOf("a"));console.log(s.substring(4,7));console.log(s.replace("a","bc"));console.log(s.split(""));console.log("2.Number-----");var n = 3.1415926;console.log(n.toFixed(3));console.log("3.Array-----");//js中的数组都是对象数组:Object[]var a1 = [1,"abc",true];console.log(a1[1]);var a2 = new Array(); //[]a2.push("hello");a2.push(123);console.log(a2[0]);var a3 = [[1,2],[3,4],[5,6]];console.log(a3[2][0]);console.log("4.数组倒转与排序-----");var arr = [6,3,9,12,5,8,4];arr.reverse();console.log(arr);//按照字符串由小到大排序//arr.sort();//按照数字由小到大排序//声明匿名函数:a,b按数字比较。arr.sort(function(a,b){return a-b;});console.log(arr);console.log("5.Math-----");console.log(Math.PI);console.log(Math.random());console.log(Math.round(Math.PI));console.log("6.Date-----");var d1 = new Date();console.log(d1);var d2 = new Date("2016/02/02 10:10:10");console.log(d2);console.log(d1.toLocaleDateString());console.log(d1.toLocaleTimeString());console.log(d1.getFullYear());console.log(d1.getMonth());//从0开始console.log("7.RegExp-----");var str = "no zuo no die, no can no bb.";var reg = /no/g;//console.log(reg.test(str));console.log(reg.exec(str));console.log(reg.exec(str));console.log(reg.exec(str));console.log(reg.exec(str));console.log(reg.exec(str));console.log(str.replace(reg, "bu"));console.log(str.match(reg));console.log(str.search(reg));</script></head><body><p>js中一切皆对象</p></body></html>









0 0
原创粉丝点击