單體內(nèi)置對象
ECMA-262對內(nèi)置對象的定義是:有ECMAScript實現(xiàn)提供的、不依賴于宿主環(huán)境的對象,這些對象在ECMAScript程序執(zhí)行之前就已經(jīng)存在了。不必顯示的實例化內(nèi)置對象,因為它們已經(jīng)實例化了,
ECMA-262定義的兩個單體內(nèi)置對象:Global和Math。
1. Global對象
Global(全局)對象,不屬于任何其它對象的屬性和方法,最終都是它的屬性和方法。所有在全局作用域中定義的屬性和函數(shù),都是Global對象的屬性。isNaN()、ifFinite()、parseInt()和parseFloat()都是Global對象的方法,Global對象還包含其他一些方法。
-
1. URI編碼方法
Global對象的encodeURI()和encodeURIcomponent()方法可以對URI(通用資源標識符)進行編碼,以便發(fā)送給瀏覽器。
var uri = "https://www.baidu.com/illegal value.htm#start";
//https://www.baidu.com/illegal%20value.htm#start
alert(encodeURI(uri));
//https%3A%2F%2Fwww.baidu.com%2Fillegal%20value.htm%23start
alert(encodeURIComponent(uri));
與之對應(yīng)的是decodeURI()和decodeURIComponent()。
var uri = "https%3A%2F%2Fwww.baidu.com%2Fillegal%20value.htm%23start";
//https%3A%2F%2Fwww.baidu.com%2Fillegal value.htm%23start
alert(decodeURI(uri));
//https://www.baidu.com/illegal value.htm#start
alert(decodeURIComponent(uri));
-
2. eval()方法
ECMAScript語言中最強的的一個方法eval()。
eval()方法就像一個完整的ECMAScript解析器,它只接受一個參數(shù),即要執(zhí)行的ECMAScript(或JavaScript)字符串。
eval("alert('hi')");
等價于
alert('hi');
當解析器發(fā)現(xiàn)代碼中調(diào)用eval()方法時,它會將傳入的參數(shù)當實際的ECMAScript語句來解析,然后把執(zhí)行結(jié)果插入到原位置。通過eval()執(zhí)行的代碼被認為是包含該次調(diào)用的執(zhí)行環(huán)境的一部分,因此被執(zhí)行的代碼具有與該執(zhí)行環(huán)境相同的作用域鏈。eval()執(zhí)行的代碼可以引用在包含環(huán)境中定義的變量。
var msg = "hello world";
eval("alert(msg)"); //"hello world"
eval("function sayHi() {alert('hi');}");
sayHi(); //"hi"
函數(shù)sayHi()是在eval()內(nèi)部定義的。但對eval()的調(diào)用最終會被替換成定義函數(shù)的實際代碼。
eval("var msg = 'hello world';");
alert('msg'); //"hello world"
在eval()中創(chuàng)建的任何變量或函數(shù)都不會被提升,因為在解析代碼的時候,它們被包含在一個字符串中;它們只在eval()執(zhí)行的時候創(chuàng)建。
嚴格模式下,在外部訪問不到eval()中創(chuàng)建的任何變量或函數(shù),因此前面兩個例子都會導(dǎo)致錯誤。在嚴格模式下,為eval賦值一會導(dǎo)致錯誤:
"use strict";
eval = "hi"; //causes error
- 3. Global對象的屬性
- undefined:特殊值undefined
- NaN:特殊值NaN
- Infinity: 特殊值Infinity
- Object:構(gòu)造函數(shù)Object
- Array:構(gòu)造函數(shù)Array
- Function:構(gòu)造函數(shù)Function
- Boolean:構(gòu)造函數(shù)Boolean
- String:構(gòu)造函數(shù)Sring
- Number:構(gòu)造函數(shù)Number
- Date:構(gòu)造函數(shù)Date
- RegExp:構(gòu)造函數(shù)RegExp
- Error:構(gòu)造函數(shù)Error
- EvalError:構(gòu)造函數(shù)EvalError
- RangeError:構(gòu)造函數(shù)RangeError
- ReferenceError:構(gòu)造函數(shù)ReferenceError
- SyntaxError:構(gòu)造函數(shù)SyntaxError
- TypeError:構(gòu)造函數(shù)TypeError
- URIError:構(gòu)造函數(shù)URIError
ECMAScript5明確禁止給undefined、NaN和Infinity賦值,這樣做即使在非嚴格模式下也會導(dǎo)致錯誤。
-
4. window對象
ECMAScript雖然沒有指出如何直接訪問Global對象,但Web瀏覽器都是將這個全局對象作為window對象的一部分加以實現(xiàn)的。因此在全局作用域中聲明的所有變量和函數(shù),都成為了window對象的屬性。
var color = "red";
function sayColor() {
alert(window.color);
}
window.sayColor(); //"red"
另一種取得Global對象的方法。
var global = function () {
return this;
}();
以上代碼創(chuàng)建了一個立即調(diào)用的函數(shù)表達式,返回this值,沒有給函數(shù)明確指定this值的情況下,this值等于Global對象 。
2. Math對象
- 1. Math對象的屬性
- Math.E:自然對數(shù)的底數(shù),即常量e的值
Math.LN10:10的自然對數(shù)
Math.LN2:2的自然對數(shù)
Math.LOG2E:以2為底e的對數(shù)
Math.LOG10E:以10為底e的對數(shù)
Math.PI:π的值
Math.SQRT1_2:1/2的平方根(2的平方根的倒數(shù))
Math.SQRT2:2的平方根
2. min()和max()方法
min()和max()方法用于確定一組數(shù)值中的最小值和最大值。這兩個方法都可以接收任意多個數(shù)值參數(shù)。
var max = Math.max(3,43,12,123);
alert(max); //123
var min =Math.min(1,31,12,3);
alert(min); //1
要找到數(shù)組中最大或最小值,可以使用apply()方法
var values = [1, 2, 3, 4, 5, 6, 7, 8];
var max = Math.max.apply(Math, values);
關(guān)鍵是把Math對象作為apply()的第一個參數(shù),從而正確的設(shè)置this值。然后,可以將然后數(shù)組做第二個參數(shù)。
-
3. 舍入方法
將小數(shù)值舍入為整數(shù)的幾個方法:Math.ceil()、Math.floor和Math.round()。
- Math.ceil()執(zhí)行向上舍入,即它總是將數(shù)值向上舍入為最接近的整數(shù)。(大)
- Math.floor()執(zhí)行向下舍入,即它總是將數(shù)值向下舍入為最接近的整數(shù)。(小)
- Math.round()執(zhí)行標準舍入,即它總是將數(shù)值四舍五入為最接近的整數(shù)。
alert(Math.ceil(25.9));//26
alert(Math.ceil(25.5));//26
alert(Math.ceil(25.1));//26
alert(Math.floor(25.9));//25
alert(Math.floor(25.5));//25
alert(Math.floor(25.1));//25
alert(Math.round(25.9));//26
alert(Math.round(25.5));//25
alert(Math.round(25.1));//25
-
4. random()方法
Math.random()方法返回大于0小于1的一個隨機數(shù)。
套用下面公式,就可以利用Math.random()從某個整數(shù)范圍內(nèi)隨機選擇一個值。
值 = Math.floor(Math.random()*可能值的總數(shù) + 第一個可能的值)
var num = Math.floor(Math.random()*10+1); //1~10
function selectFrom(lowerValue,upperValue) {
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random()*choices + lowerValue);
}
var num = selectFrom(2,10);
alert(num); //2~10(包括2和10)
var colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"];
var color = colors[selectFrom(0,colors.lengh-1)];
alert(color); //可能是數(shù)組中包含的任何一個字符串
- 5. 其他方法
- Math.abs(num):返回num的絕對值
- Math.exp(num):放回Math.E的num次冪
- Math.log(num):返回num的自然對數(shù)
- Math.pow(num,power):返回num的power次冪
- Math.sqrt(num):返回num的平方根
- Math.acos(x):返回x的反余弦值
- Math.asin(x):返回x反正弦值
- Math.atan(x):返回x的反正切值
- Math.atan2(y,x):返回y/x的反正切值
- Math.cos(x):返回x的余弦值
- Math.sin(x):返回x的正弦值
- Math.tan(x):返回x的正切值