单体内置对象
Global 对象
- 对象方法
1. URI 编码方法:
- encodeURI():用特殊的 UTF-8 编码替换所有无效的字符,让浏览器能够接受和理解;
不会对本身属于 URI 的特殊字符进行编码,例如冒号、正斜杠、问号和井字号
var uri = "http://www.wrox.com/illegal value.htm#start";
encodeURI(uri); // 返回 "http://www.wrox.com/illegal%20value.htm#start"
- encodeURIComponent():用特殊的 UTF-8 编码替换所有无效的字符,让浏览器能够接受和理解;
会对它发现的任何非标准字符进行编码
var uri = "http://www.wrox.com/illegal value.htm#start";
encodeURIComponent(uri); // 返回 "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"
- decodeURI(): 只能对使用 encodeURI() 替换的字符进行解码
var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start";
decodeURI(uri); // 返回 http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start
- decodeURIComponent(): 能够解码使用 encodeURIComponent() 编码的所有字符
var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start";
decodeURIComponent(uri); // 返回 http://www.wrox.com/illegal value.htm#start
2. eval() 方法:
eval("function sayHi() { alert('hi'); }");
sayHi(); // "hi"
Math 对象
- 对象属性
Math.E; // 自然对数的底数,即常量 e 的值
Math.LN10; // 10的自然对数
Math.LN2; // 2的自然对数
Math.LOG2E; // 以2为底 e 的对数
Math.LOG10E; // 以10为底 e 的对数
Math.PI; // π的值
Math.SQRT1_2; // 1/2的平方根(即2的平方根的倒数)
Math.SQRT2; // 2的平方根
- 对象方法
1. min() 和 max() 方法:接收任意多个数值参数,max()返回最大值,min()返回最小值
var max = Math.max(3, 54, 32, 16);
alert(max); // 54
var min = Math.min(3, 54, 32, 16);
alert(min); // 3
2. 舍入方法:
Math.ceil() :向上舍入,即它总是将数值向上舍入为最接近的整数;
Math.floor() :向下舍入,即它总是将数值向下舍入为最接近的整数;
Math.round() :标准舍入,即它总是将数值四舍五入为最接近的整数
alert(Math.ceil(25.9)); // 26
alert(Math.ceil(25.5)); // 26
alert(Math.ceil(25.1)); // 26
alert(Math.round(25.9)); // 26
alert(Math.round(25.5)); // 26
alert(Math.round(25.1)); // 25
alert(Math.floor(25.9)); // 25
alert(Math.floor(25.5)); // 25
alert(Math.floor(25.1)); // 25
3. random() 方法:返回大于等于 0 小于 1 的一个随机数
var num = Math.floor(Math.random() * 10 + 1); // [1,10]之间的整数
var num = Math.floor(Math.random() * 9 + 2); // [2,10]之间的整数
4. 其他
Math.abs(num); // 返回 num 的绝对值
Math.asin(x); // 返回 x 的反正弦值
Math.exp(num); // 返回 Math.E 的 num 次幂
Math.atan(x); // 返回 x 的反正切值
Math.log(num); // 返回 num 的自然对数
Math.atan2(y,x); // 返回 y/x 的反正切值
Math.pow(num,power); // 返回 num 的 power 次幂
Math.cos(x); // 返回 x 的余弦值
Math.sqrt(num); // 返回 num 的平方根
Math.sin(x); // 返回 x 的正弦值
Math.acos(x); // 返回 x 的反余弦值
Math.tan(x); // 返回 x 的正切值