5.7 Global对象
encodeURI encodeURIComponent decodeURI decodeURIComponent
用特殊的utf-8编码替换所有无效的字符,从而让浏览器能理解
var uri = "http://www.baidu.com/illegal value.html#start";
l(encodeURI(uri));//http://www.baidu.com/illegal value.html#start
l(encodeURIComponent(uri));//http%3A%2F%2Fwww.baidu.com%2Fillegal%20value.html%23start
l(decodeURI(uri));//http://www.baidu.com/illegal value.html#start
l(decodeURIComponent(uri));//http://www.baidu.com/illegal value.html#start
eval 像一个完整的ECMAScript解析器
严格模式下可能,外部访问不了eval中创建的任何变量和参数
Gobal对象的属性
undefined NaN Infinity Object Array Function Boolean String Number
Date RegExp Error EvalError RangeError ReferenceError SyntaxError TypeError URIError
window 对象
在全局作用域中声明的所有变量和函数,都变成了window对象属性
var color = "red";
function sayColor(){
l(window.color);
}
window.sayColor();//red
var global = function(){
return this;
}
Math 对象的属性
Math.e Math.LN10 Math.LN2 Math.LOG2E Math.LOG10e Math.PI Math.SQRT1_2 Math.SQRT2
min max 方法
var max = Math.max(3, 54, 32, 16); l(max);//54 var min = Math.min(3, 54, 32, 16); l(min);//3 //这个技巧的关键是把Math对象作为apply()的第一个参数,从而正确设置this var values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var max = Math.max.apply(Math, values); l(max);//10
ceil floor round
l(Math.ceil(25.9)); //26 l(Math.ceil(25.5));// 26 l(Math.ceil(25.1));// 26 l(Math.floor(25.9));//25 l(Math.floor(25.5));//25 l(Math.floor(25.1));//25 l(Math.round(25.9));//26 l(Math.round(25.5));//26 l(Math.round(25.1));//25
random 方法
// 1-10 var num = Math.floor(Math.random() * 10 +1); l(num); // 2-10 var num = Math.floor(Math.random() * 9 + 2); l(num); // 随机函数 function selectForm(lowerValue, upperValue){ var choices = upperValue - lowerValue + 1; return Math.floor(Math.random()* choices + lowerValue); } var num = selectForm(2, 10); l(num); var colors = [‘red‘, ‘green‘, ‘blue‘, ‘yellow‘, ‘black‘, ‘purple‘, ‘brown‘]; var color = colors[selectForm(0, colors.length - 1)]; l(color);
其他方法
Math.abs() Math.exp() Math.log() Math.pow() Math.sqrt() Math.acos()