一、Object类型
创建Object实例的方式:
1、使用new操作符后加Object构造函数
var person = new Object();
person.name = "Nicholas";
person.age = 27;
2、使用对象字面量表示法(推荐)
var person = {name: "Nicholas",age: 29
};
上例左边的花括号表示对象字面量的开始,因为它出现在表达式上下文中。而表达式上下文指的是该上下文期待一个值(表达式),赋值操作符表示后面是一个值,所以左花括号这里表示一个表达式的开始。所以每一个键值对用逗号隔开。
同样的花括号出现在一个语句上下文中,如if语句条件的后面,则表示语句块的开始。所以每一个语句后要加分号。
使用对象字面量传递可选参数:
function displayInfo(args) {var output = "";if (typeof args.name === "string") {output += "Name: " + args.name + "\n";}if (typeof args.age === "number") {output += "Age: " + args.age + "\n";}alert(output);
}
displayInfo({name: "Nicholas",age: 29
});
//对那些必需值使用命名参数,对于多个可选参数可以用对象字面量来封装
访问对象属性:
//点表示法
person.name
//方括号语法
person["name"]
方括号语法的优点在于可以通过变量来访问属性,还有在属性名有空格或者是关键字或保留字时使用。 (建议一般使用点表示法)
二、Array类型
ECMAScript数组的每一项可以保存任何类型的数据。
ECMAScript数组的大小是可以动态调整的。
创建Array数组的两种基本方式:
1、使用Array构造函数
var colors = new Array();
var colors = new Array(20); //创建一个包含20项的数组
var colors = new Array("red", "blue", "green"); //创建一个包含三个字符串的数组
var colors = Array(); //也可以省略new操作符
2、使用数组字面量表示法
var colors = ["red", "blue", "green"];
var values = [1, 2,]; //不要这样,这会创建一个包含2或3项的数组
数组的length属性的应用:
1、从数组的末尾移除项或者向数组中添加新项
var colors = ["red", "blue", "green"];
colors.length = 2;
alert(colors[2]); //undefined
//移除末项var colors = ["red", "blue"];
colors.length = 3;
alert(colors[2]); //undefined
//增加新项,不过新项都是未定义
2、在数组末尾添加新项值
var colors = ["red", "blue", "green"];
colors[colors.length] = "black"; //在后面添加一种颜色
colors[colors.length] = "brown"; //再在后面添加一种颜色
//数组长度值等于最后索引加1,即最后索引等于长度值减1
检测数组
if (Array.isArray(value)) {//dosomething
}
转换方法
var colors = ["red", "blue", "green"];
alert(colors.toString()); //red,blue,green
alert(colors.valueOf()); //red,blue,green
alert(colors); //red,blue,green
//alert要接收字符串参数,它会在后台调用toString()方法
使用toString()、valueOf()、toLocaleString()方法在默认情况下都会以逗号分隔的字符串的形式返回数组,而join()方法,则可以使用不同的分隔符来构建这个字符串
var colors = ["red", "blue", "green"];
alert(colors.join(",")); //red,blue,green
alert(colors.join("||")); //red||blue||green
alert(colors.join()); //red,blue,green(使用默认的)
栈方法
栈是一种后进先出(last-in-first-out)的数据结构
push()方法可以接收任意数量的参数,把它们逐个添加到数组的末尾,并返回修改后数组的长度
pop()方法从数组末尾移除最后一项,减少数组的length值,然后返回移除的项
var colors = new Array();
var count = colors.push("red", "blue");
alert(count); //2(是长度)count = colors.push("black");
alert(count); //3var item = colors.pop();
alert(item); //"black"(被移除的项)
alert(colors.length); //2
队列方法
队列是一种先进先出的数据结构
shift()方法移除数组的第一项,减少数组的length值,然后返回移除的项
unshift()方法可以接收任意数量的参数,把它们逐个添加到数组的开头,并返回修改后数组的长度
重排序方法
reverse()方法会反转数组项的顺序
sort()方法会按升序排列数组项(先调用toString()转型方法,然后比较得到的字符串)
var values = [0, 1, 5, 10, 15];
alert(values.sort()); //0,1,10,5,15
比较函数
function compare(value1, value2) {if (value1
}var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values); //0,1,5,10,15
也可以这样(只能用于数值比较)
function compare(value1, value2) {return value1 - value2;
}
var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values); //0,1,5,10,15
操作方法
concat()方法,先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组。而原来的数组值不变
var colors = ["red", "blue", "green"];
var colors2 = colors.concat("yellow", ["black", "brown"]);
alert(colors); //red,blue,green
alert(colors2); //red,blue,green,yellow,black,brown
slice()方法,基于当前数组中的一个或多个项创建一个新数组。原数组值不变
var colors = ["red", "blue", "green", "yellow", "purple"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1, 4);
alert(colors); //red,blue,green,yellow,purple
alert(colors2); //blue,green,yellow,purple
alert(colors3); //blue,green,yellowslice(-2, -1)等同于slice(3, 4) //负数加长度
splice()方法
//删除(参数1要删除的位置,参数2要删除的项数)
var colors = ["red", "green", "blue"];
var removed = colors.splice(0, 1);
alert(colors); //green,blue
alert(removed); //red
//插入(参数1要插入的位置,参数2要删除的项数,参数3+要插入的项)
removed = colors.splice(1, 0, "yellow", "orange");
alert(colors); //green,yellow,orange,blue
alert(removed); //空数组
//替换(类似插入)
removed = colors.splice(1, 1, "red", "purple");
alert(colors); //green,red,purple,orange,blue
alert(removed); //yellow
位置方法
indexOf()方法从数组的开头开始向后查找,返回要查找的项在数组中的位置
lastIndexOf()方法从数组的末尾开始向后查找返回要查找的项在数组中的位置
//参数1要查找的项,参数2查找的起点位置
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
alert(numbers.indexOf(4)); //3
alert(numbers.lastIndexOf(4)); //5
alert(numbers.indexOf(4, 4)); //5
alert(numbers.lastIndexOf(4, 4)); //3
迭代方法
- every():如果该函数对每一项都返回true,则返回true
- filter():返回该函数会返回true的项组成的数组(适合查询符合条件的数组)
- forEach():没有返回值(与for类似)
- map():返回每次函数调用的结果组成的数组(适合创建与之一一对应的新数组)
- some():如果该函数对任一项返回true,则返回true(与every()相反)
以上方法都不会修改数组中包含的值
//每个方法接收两个参数:要在每一项运行的函数和运行该函数的作用域对象
//函数接收三个参数:数组项的值(item)、该项在数组中的位置(index)、数组对象本身(array)
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
var everyResult = numbers.every(function(item, index, array){return (item > 2); //false
});
var someResult = numbers.some(function(item, index, array){return (item > 2); //true
});
var filterResult = numbers.filter(function(item, index, array){return (item > 2); //[3,4,5,4,3]
});
var mapResult = numbers.map(function(item, index, array){return item * 2; //[2,4,6,8,10,8,6,4,2]
});
归并方法
reduce()方法,从数组的第一项开始,逐个遍历到最后,构建一个最终的返回值
reduceRight()方法,从数组的最后一项开始,向前遍历到第一项
//每个方法接收两个参数:要在每一项运行的函数和作为归并基础的初始值
//函数接收四个参数:前一个值、当前值、项的索引和数组对象
var values = [1, 2, 3, 4, 5];
var sum = values.reduce(function(prev, cur, index, array){return prev + cur; //15
});
//第一次prev是1,cur是2;第二次prev是3(1+2),cur是3(数组第三项);持续到遍历数组,最后返回结果
三、Date类型
创建当前日期和对象
var now = new Date();
Date构造函数如果传参数,必须是该日期的毫秒数(从UTC时间1970年1月1日午夜起至该日期止的毫秒数)
Date.parse()接收一个表示日期的字符串参数(日期格式不定),根据这个参数返回相应日期的毫秒数
var someDate = new Date(Date.parse("May 25, 2004"))
//等同于
var someDate = new Date("May 25, 2004")
//直接将字符串传递给Date构造函数,也会在后台调用Date.parse()
Date.UTC()接收的参数分别是年、基于0的月、天、小时、分、秒及毫秒,根据这个参数返回相应日期的毫秒数
var someDate = new Date(Date.UTC(2005, 4, 5, 17, 55, 55));
//等同于
var someDate = new Date(2005, 4, 5, 17, 55, 55); //2015年5月5日17:55:55
//直接将数字传递给Date构造函数,也会在后台调用Date.UTC()
Date.now()表示调用这个方法的日期和时间的毫秒数
var start = Date.now();
doSomething();
var end = Date.now();
result = end - start;
不支持此方法的浏览器,可以在Date对象前使用+操作符
var start = +Date.now();
doSomething();
var end = +Date.now();
result = end - start;
四、RegExp类型
//字面量形式
var expression = / pattern / flags ;
//RegExp构造函数形式(注意双重转义)
var expression = new RegExp("pattern", "flags");
正则表达式的匹配模式支持三个标志:
- g:全局模式,即模式被应用于所有字符串,而非在发现第一个匹配项时就停止
- i:不区分大小写模式,即在确定匹配项时忽略模式与字符串的大小写
- m:多行模式,即在到达一行文本末时,还会继续查找下一行
模式中的所有元字符都必须转义:
( [ { \ ^ $ | ) ? * + . ] }
RegExp实例属性:
- global:布尔值,表示是否设置了g标志
- ignoreCase:布尔值,表示是否设置了i标志
- lastIndex: 整数,表示开始搜索下一个匹配项的字符位置,从零算起
- multiline:布尔值,表示是否设置了m标志
- source:正则表达式的字符串表示,按照字面量形式而非构造函数形式返回(不论实例是哪种形式)
RegExp实例方法:
exec()方法
接收一个参数,即要应用模式的字符串,然后返回包含第一个匹配项信息的数组。
返回的数组包含两个额外的属性:index(匹配项在字符串中的位置)和input(应用正则表达式的字符串)。
在数组中,第一项是与整个模式匹配的字符串,其他项是与模式中的捕获组匹配的字符串。
var text = "mom and dad and baby";
var pattern = /mom( and dad ( and baby)?)?/gi;
var matches = pattern.exec(text);
alert(matches.index); //0
alert(matches.input); //"mom and dad and baby"
alert(matches[0]); //"mom and dad and baby"
alert(matches[1]); //" and dad and baby"
alert(matches[2]); //" and baby"
exec()方法即使在设置了g,每次也只会返回一个匹配项。
不设置g,在同一字符串上多次调用exec()将始终返回第一个匹配项;
设置g,每次调用exec()都会在字符串中继续查找匹配项。
var text = "cat, bat, sat, fat";
var pattern1 = /.at/;var matches = pattern1.exec(text);
alert(matches.index); //0
alert(matches[0]); //cat
alert(matches.lastIndex); //0matches = pattern1.exec(text);
alert(matches.index); //0
alert(matches[0]); //cat
alert(matches.lastIndex); //0var pattern2 = /.at/g;var matches = pattern2.exec(text);
alert(matches.index); //0
alert(matches[0]); //cat
alert(matches.lastIndex); //3matches = pattern2.exec(text);
alert(matches.index); //5
alert(matches[0]); //bat
alert(matches.lastIndex); //8
test()方法
接收一个字符串参数,返回布尔值。通常用于if语句中,用于验证信息是否有效
var text = "000-00-0000";
var pattern = /\d{3}-\d{2}-\d{4}/;
if (pattern.test(text)) {alert("The pattern was matched.");
}
五、Function类型
//函数声明语法
function sum(num1, num2) {return num1 + num2;
}
//函数表达式语法
var sum = function(num1, num2) {return num1 + num2;
}
每个函数都是Function类型的实例,都是对象,因此函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定
function sum(num1, num2) {return num1 + num2;
}
alert(sum(10, 10)); //20var anotherSum = sum; //不带圆括号的函数名是访问函数指针,而不是调用函数
alert(anotherSum(10, 10)); //20sum = null; //让sum与函数“断绝关系”
alert(anotherSum(10, 10)); //20
函数没有重载,即如果声明两个同名的函数,后面的函数会覆盖前面的函数
函数声明提升,解析器会率先读取函数声明,并使其在执行任何代码之前可用。至于函数表达式,则必须等到解析器执行到它所在的代码行,才会被解释执行
作为值的函数,因为函数名本身就是变量,所以函数也可以作为值来使用,把一个函数作为参数传给另一个函数,也可以从一个函数中返回另一个函数
eg:有一个对象数组,要根据对象属性对数组进行排序
函数内部两个特殊的对象(arguments、this)及属性(caller):
arguments.callee(),指向拥有arguments对象的函数,callee属性是个指针
function func1(num) {if (num <&#61; 1) {return 1;} else {return num * func1(num-1)}
}
alert(func1(5)); //120
var func3 &#61; func1;
func1 &#61; function() {return 0;
};
alert(func3(5)); //0&#xff08;因为func1后来变成了0&#xff09;
//上例函数的执行与函数名func1紧紧耦合
//下例消除紧密耦合现象
function func2(num) {if (num <&#61; 1) {return 1;} else {return num * arguments.callee(num-1)}
}
alert(func2(5)); //120
var func4 &#61; func2;
func2 &#61; function() {return 0;
};
alert(func4(5)); //120&#xff08;func2后面的操作不影响func4&#xff09;
this对象&#xff0c;this引用的是函数据以执行的环境对象&#xff08;全局作用域调用函数时&#xff0c;this对象引用的就是window&#xff09;
window.color &#61; "red";
var o &#61; {color: "blue"};
function sayColor() {alert(this.color);
}
sayColor(); //red&#xff08;全局作用域中&#xff0c;全局对象window&#xff09;
o.sayColor &#61; sayColor;
o.sayColor(); //blue&#xff08;this引用的是对象o&#xff09;
函数对象的属性caller&#xff08;重点是函数&#xff09;&#xff0c;保存着调用当前函数的函数引用&#xff08;如果是在全局作用域中调用当前函数&#xff0c;值为null&#xff09;
function outer() {inner();
}
function inner() {alert(arguments.callee.caller);
}
outer(); //function outer() {inner();}&#xff08;格式与这个不一样&#xff09;
注意分清arguments.caller
和函数的caller属性&#xff0c;前者在严格模式下会导致错误
函数属性和方法
length属性&#xff0c;表示函数命名参数的个数&#xff08;不是传递的参数的个数&#xff09;
prototype属性&#xff0c;是保存所有实例方法的真正所在&#xff0c;不可枚举&#xff0c;无法使用for-in实现
apply()方法和call()方法&#xff0c;用途是在特定的作用域中调用函数&#xff0c;两方法除了接收参数的方式不同&#xff0c;其它都一样
//apply()接收两个参数&#xff1a;运行函数的作用域和参数数组
//参数数组可以是Array的实例&#xff0c;也可以是arguments对象
function sum(num1, num2) {return num1 &#43; num2;
}
function callSum1(num1, num2) {return sum.apply(this, arguments); //arguments对象
}
function callSum2(num1, num2) {return sum.apply(this, [num1, num2]); //Array实例
}
alert(callSum1(10, 10)); //20
alert(callSum2(10, 10)); //20
//因为在全局作用域中调用sum&#xff0c;所以传入的是window对象//call()也接收两个参数&#xff0c;但是第二个参数必须逐个列举出来
function sum(num1, num2) {return num1 &#43; num2;
}
function callSum(num1, num2) {return sum.call(this, num1, num2);
}
alert(callSum(10, 10)); //20
主要用途是能够扩充函数赖以执行的作用域
window.color &#61; "red";
var o &#61; {color: "blue"};
function sayColor() {alert(this.color);
}
sayColor(); //red&#xff08;全局作用域中调用函数&#xff09;
sayColor.call(this); //red&#xff08;全局作用域中调用函数&#xff09;
sayColor.call(window); //red&#xff08;全局作用域中调用函数&#xff09;
sayColor.call(o); //blue&#xff08;在对象o中调用函数&#xff09;
bind()方法&#xff0c;会创建一个函数的实例&#xff0c;其this值会被绑定到传给bind()函数的值
window.color &#61; "red";
var o &#61; {color: "blue"};
function sayColor() {alert(this.color);
}
var objectSayColor &#61; sayColor.bind(o);
objectSayColor(); //blue
每个函数的toString()、toLocaleString()、valueOf()方法都返回函数的代码&#xff0c;格式因浏览器而异
六、基本包装类型
包括Boolean、Number、String
var s1 &#61; "some text";
var s2 &#61; s1.substring(2);
//实际有三步操作
1、创建String类型的一个实例
2、在实例上调用指定的方法
3、销毁实例
var s1 &#61; new String("some text");
var s2 &#61; s1.substring(2);
s1 &#61; null;
基本包装类型不同于引用类型之处在于对象的生存期&#xff0c;只存在于一行代码的执行瞬间&#xff0c;然后立即被销毁
var s1 &#61; "some text";
s1.coloe &#61; "red";
alert(s1.color); //undefined
//第二行创建的对象已经被销毁&#xff0c;第三行又创建对象&#xff0c;但是还没有color属性
1、Boolean类型
var booleanObject &#61; new Boolean(true);
建议永远不要使用Boolean对象
基本类型的布尔值与引用类型的布尔值的区别
//Boolean对象与Boolean值
var falseObject &#61; new Boolean(false);
var result &#61; falseObject && true;
alert(result); //true&#xff08;因为falseObject是对象&#xff0c;所以为true&#xff09;
var falseValue &#61; false;
result &#61; falseValue && true;
alert(result); //falsealert(typeof falseObject); //object
alert(typeof falseValue); //booleanalert(falseObject instanceof Boolean); //true
alert(falseValue instanceof Boolean); //false
2、Number类型
var numberObject &#61; new Number(10);
不建议使用Number对象
var num &#61; 10;
alert(num.toString()); //"10"
alert(num.valueOf()); //10
//按照指定的小数位返回数值的字符串表示
var num &#61; 10;
alert(num.toFixed(2)); //"10.00"
//包含的小数位比指定的多时&#xff0c;进行四舍五入
var num &#61; 10.004;
alert(num.toFixed(2)); //"10.00"
//返回以指数表示法表示的数值的字符串形式&#xff0c;参数指定输出结果中的小数位数
var num &#61; 100000;
alert(num.toExponential(1)); //"1.0e&#43;5"
//可能返回固定大小的格式&#xff0c;也可能返回指数格式&#xff0c;参数表示所有数字的位数&#xff08;不包括指数部分&#xff09;
var num &#61; 99;
alert(num.toPrecision(1)); //"1e&#43;2"
alert(num.toPrecision(2)); //"99"
alert(num.toPrecision(3)); //"99.0"
3、String类型
var stringObject &#61; new String("hello world");
字符方法
var stringValue &#61; "hello world";
alert(stringValue.charAt(1)); //"e"&#xff08;返回给定位置的那个字符&#xff09;
alert(stringValue.charCodeAt(1)); //"101"(返回给定位置的字符的字符编码)
alert(stringValue[1]); //"e"&#xff08;方括号加数字索引&#xff09;
字符串操作方法
//拼接字符串&#xff08;类似于加号操作符&#xff08;&#43;&#xff09;加号用的更多更方便&#xff09;
var stringValue &#61; "hello ";
var result &#61; stringValue.concat("world"); //"hello world"
result &#61; stringValue.concat("world", "!"); //"hello world!"&#xff08;多个参数&#xff09;
alert(stringValue); //"hello"&#xff08;没变&#xff09;//创建子字符串&#xff0c;接收两个参数&#xff1a;开始位置&#xff08;包括&#xff09;和结束位置&#xff08;不包括&#xff09;&#xff08;substr&#xff08;&#xff09;除外&#xff09;
var stringValue &#61; "hello world";
alert(stringValue.slice(3, 7)); //"lo w"
alert(stringValue.substring(3, 7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl" &#xff08;第二个参数是子字符的个数&#xff09;
//负值
var stringValue &#61; "hello world";
alert(stringValue.slice(-3)); //"rld"&#xff08;负值与长度相加&#xff09;
alert(stringValue.substring(-3)); //"hello world"
alert(stringValue.substr(-3)); //"rld"&#xff08;第一个参数负值加长度&#xff09;
alert(stringValue.slice(3, -4)); //"lo w"
alert(stringValue.substring(3, -4));//"hel"&#xff08;负值都转换为0&#xff09;
alert(stringValue.substr(3, -4)); //""&#xff08;第二个参数负值转换为0&#xff09;//以上方法对原始字符串没有任何影响
字符串位置
var stringValue &#61; "hello world";
alert(stringValue.indexOf("o")); //4(从开头向后)
alert(stringValue.lastIndexOf("o")); //7(从末尾向前)
//第二个参数是从哪个位置开始
alert(stringValue.indexOf("o", 6)); //7
alert(stringValue.lastIndexOf("o", 6)) //4
通过循环调用indexof()或lastIndexOf()来找到所有匹配的子字符串
var stringValue &#61; "jions ronwfownk fwiuh rwjfohfo rwjohjvo"
var position &#61; new Array();
var pos &#61; stringValue.indexOf("o");
while (pos > -1) {position.push(pos);pos &#61; stringValue.indexOf("o", pos &#43; 1);
}
console.log(position); //[2, 7, 11, 26, 29, 34, 38]
trim()方法&#xff0c;创建一个字符串的副本&#xff0c;删除前置及后缀的空格&#xff0c;然后返回结果&#xff0c;不影响原始字符串
字符串的大小写转换方法&#xff1a;toLowerCase()、toUpperCase()
字符串的模式匹配方法&#xff1a;
- match()&#xff0c;类似RegExp的exec()方法
- search()&#xff0c;返回第一个匹配项的索引
- replace()&#xff0c;替换子字符串&#xff0c;第一个参数是正则表达式&#xff0c;第二个参数是补充的字符串或函数
注意全局正则表达式可以替换所有子字符串
var text &#61; "cat , bat, sat, fat";
var result &#61; text.replace("at", "ond");
console.log(result); //cond , bat, sat, fat
result &#61; text.replace(/at/g, "ond");
console.log(result); //cond , bond, sond, fond
split()&#xff0c;基于指定的分隔符将一个字符串分割成多个子字符串&#xff0c;并将结果放在一个数组中
var colorText &#61; "red,blue,green,yellow";
var colors1 &#61; colorText.split(",");
var colors2 &#61; colorText.split(",", 2);
var colors3 &#61; colorText.split(/[^\.]&#43;/);
alert(colors1); //["red", "blue", "green", "yellow"]
alert(colors2); //["red", "blue"]&#xff08;指定数组的大小&#xff09;
localeCompare()&#xff1a;
- 如果字符串在字母表中排在字符串参数之前&#xff0c;返回-1
- 如果字符串在字母表中排在字符串参数之后&#xff0c;返回1
- 相等&#xff0c;返回0
如下
var stringValue &#61; "yellow";
alert(stringValue.localeCompare("brick")); //1
alert(stringValue.localeCompare("yellow")); //0
alert(stringValue.localeCompare("zoo")); //-1
因为localeCompare()返回的数值取决于实现&#xff0c;所以最好用函数使用这个方法
function determineOrder(value) {var result &#61; stringValue.localeCompare(value);if (result <0) {console.log("yellow在" &#43; value &#43; "之前");} else if (result > 0) {console.log("yellow在" &#43; value &#43; "之后");} else {console.log("相等");}
}
determineOrder("brick"); //yellow在brick之后
determineOrder("yellow"); //相等
determineOrder("zoo"); //yellow在zoo之前
七、单体内置对象
1、Global对象
所有在全局作用域中定义的属性和函数&#xff0c;都是Global对象的属性和方法&#xff08;之前的isNaN()、isFinite()、parseInt()、parseFloat()都是Global对象的方法&#xff09;
URI编码方法
encodeURI()主要用于整个URI&#xff0c;而encodeURIComponent()主要用于对URI中的某一段进行编码
主要区别是&#xff0c;encodeURI()不会对本身属于URI的特殊字符进行编码&#xff0c;例如冒号、正斜号、问号、井号&#xff0c;而encodeURIComponent()则会对它发现的任何非标准字符进行编码
var uri &#61; "http://www.baudu.com/illegal value.htm#start";
console.log(encodeURI(uri));
//http://www.baudu.com/illegal%20value.htm#start
console.log(encodeURIComponent(uri));
//http%3A%2F%2Fwww.baudu.com%2Fillegal%20value.htm%23start
所以对于整个URI使用encodeURI()&#xff0c;对于附加在URI后面的字符串使用encodeURIComponent()
- decodeURI()只能对使用encodeURI()替换的字符进行解码
- decodeURIComponent()能够对使用encodeURIComponent()编码的所有字符进行解码&#xff0c;即它可以解码任何特殊字符的编码
eval()方法
就是一个完整的ECMAScript解析器&#xff0c;只接收一个参数&#xff0c;即要执行的ECMAScript字符串
在eval()中创建的任何变量或函数都不会提升
严格模式下&#xff0c;在外部访问不到eval()中创建的任何变量或函数
2、Math对象
min()和max()方法
//找到一组数值中的最大值
var max &#61; Math.max(3, 54, 32, 16);
alert(max); //54
//找到数组中的最大值
var values &#61; [1, 2, 3, 4, 5, 6, 7, 8, 9];
var max &#61; Math.max.apply(Math, values); //9
//把Math对象作为第一个参数&#xff0c;从而正确地设置this值
舍入方法
- Math.ceil()&#xff0c;执行向上舍入
- Math.floor()&#xff0c;执行向下舍入
- Math.round()&#xff0c;执行四舍五入
random()方法
Math.random()方法返回大于等于0小于1的一个随机数
//从某个整数范围内随机选择一个值
值 &#61; Math.floor(Math.random() * 可能值的总数 &#43; 第一个可能值);//选择1-10之间的数值
var num &#61; Math.floor(Math.random() * 10 &#43; 1);//选择2-10之间的数值
var num &#61; Math.floor(Math.random() * 9 &#43; 2);//通过函数
function selectFrom(lowerValue, upperValue) {var choices &#61; upperValue - lowerValue &#43; 1;return Math.floor(Math.random() * choices &#43; lowerValue);
}
alert(selectFrom(2, 10)); //选择2-10之间的数值//利用函数从数组中随机取出一项
function selectFrom(lowerValue, upperValue) {var choices &#61; upperValue - lowerValue &#43; 1;return Math.floor(Math.random() * choices &#43; lowerValue);
}
var colors &#61; ["red", "green", "blue", "yellow", "black", "purple"];
var color &#61; colors[selectFrom(0, colors.length-1)];
alert(color);