JavaScript中定義了兩個(gè)單體內(nèi)置對(duì)象:Global和Math。
Global對(duì)象
Global對(duì)象是JavaScript中最特別的一個(gè)對(duì)象。不屬于任何其他對(duì)象的屬性和方法,最終都是它的屬性和方法。實(shí)際上,沒(méi)有全局變量或全局作用域,所有在全局作用域中定義的屬性和函數(shù),都是Global對(duì)象的屬性。
Global對(duì)象包含了一些有用的方法:
1.URI編碼方法
Global對(duì)象的encodeURI()和encodeURIComponent()方法可以對(duì)URI進(jìn)行編碼,encodeURI()主要用于整個(gè)URI,而encodeURIComponent()主要用于對(duì)URI中的某一段進(jìn)行編碼。
var uri = "http://www.jb51 xxyh.com#login";alert(encodeURI(uri)); // "http://www.jb51%20xxyh.com#login"alert(encodeURIComponent(uri)); // "http%3A%2F%2Fwww.jb51%20xxyh.com%23login"
encodeURI()不會(huì)對(duì)本身屬于URI的特殊字符進(jìn)行編碼(如,冒號(hào)、正斜杠、問(wèn)號(hào)和井號(hào)),encodeURIComponent會(huì)對(duì)發(fā)現(xiàn)的任何非標(biāo)準(zhǔn)字符進(jìn)行編碼。
與encodeURI()和encodeURIComponent()對(duì)應(yīng)的有兩個(gè)解碼方法decodeURI和decodeURIComponent
var uri = "http%3A%2F%2Fwww.jb51%20xxyh.com%23login";alert(decodeURI(uri)); // "http%3A%2F%2Fwww.jb51 xxyh.com%23login"alert(decodeURIComponent(uri)); // http://www.jb51 xxyh.com#login
其中,decodeURI()只能對(duì)使用encodeURI()替換的字符進(jìn)行解碼。decodeURIComponent能夠?qū)ncodeURIComponent()進(jìn)行解碼。
2.eval()方法
eval()只接受一個(gè)參數(shù),即要執(zhí)行的JavaScript字符串,例如:
eval("alert('hello')");
上面這行代碼等價(jià)于:
alert("hello");
當(dāng)解析器調(diào)用eval()方法時(shí),會(huì)將傳入的參數(shù)作為實(shí)際的JavaScript語(yǔ)句解析,然后將執(zhí)行結(jié)果插入原來(lái)的位置。通過(guò)eval()執(zhí)行的代碼被認(rèn)為是包含該次調(diào)用的執(zhí)行環(huán)境的一部分,因此被執(zhí)行的代碼具有與該執(zhí)行環(huán)境相同的作用域鏈。這意味著通過(guò)eval()執(zhí)行的代碼可以引用在包含環(huán)境中定義的變量。
var msg = "good morning";eval("alert(msg)"); // "good morning"
同樣地,可以在eval()中定義一個(gè)函數(shù),然后再在該調(diào)用的外部引用這個(gè)函數(shù):
eval("function sayHi() {alert('hello')}");
對(duì)于變量也是一樣:
eval("var msg = 'hello world'");alert(msg); // "hello world"
在eval()中創(chuàng)建的任何變量或函數(shù)都不會(huì)被提升,在解析代碼時(shí),它們被包含在一個(gè)字符串中;只有在eval()執(zhí)行時(shí)才創(chuàng)建。
3.window對(duì)象
JavaScript沒(méi)有指出如何直接訪問(wèn)Global對(duì)象,但是web瀏覽器都是將它作為window對(duì)象的一部分加以實(shí)現(xiàn)的。因此,在全局作用域中聲明的所有變量和函數(shù),都稱為window對(duì)象的屬性。
var color = "red";function sayColor() { alert(window.color);}window.sayColor();
上面定義了一個(gè)全局變量color和全局函數(shù)sayColor()方法,在函數(shù)內(nèi)部通過(guò)window.color來(lái)訪問(wèn)color變量,說(shuō)明全局變量color是window對(duì)象的屬性。然后通過(guò)window.sayColor()來(lái)調(diào)用sayColor()方法,說(shuō)明sayColor()是window對(duì)象的方法。
取得Global對(duì)象的方法:
var global = function () { return this;}();
Math對(duì)象
JavaScript提供了Math對(duì)象,用于提供快速的計(jì)算功能。
1.Math對(duì)象的屬性
Math對(duì)象的屬性大多是一些數(shù)學(xué)計(jì)算中的特殊值。
2.min()和max()方法
min()和max()方法用于確定一組數(shù)值中的最小值和最大值。這兩個(gè)方法都可以接收任意多個(gè)數(shù)值參數(shù)。
var max = Math.max(4,89,65,34);alert(max); // 89var min = Math.min(4,89,65,34);alert(min);
查找數(shù)值中的最大值和最小值,可以使用如下的方式調(diào)用apply()方法:
var values = [4,89,65,34];var max = Math.max.apply(Math, values);
3.舍入方法
• Math.ceil():向上舍入,即進(jìn)一法,只要小數(shù)位不為0就向上取整
• Math.floor():向下舍入,即取整法,舍去小數(shù)位
• Math.round():標(biāo)準(zhǔn)舍入,即四舍五入法
示例:
alert(Math.ceil(11.4)); // 12alert(Math.ceil(11.5)); // 12alert(Math.ceil(11.8)); // 12alert(Math.floor(11.4)); // 11alert(Math.floor(11.5)); // 11alert(Math.floor(11.8)); // 11alert(Math.round(11.4)); // 11alert(Math.round(11.5)); // 12alert(Math.round(11.8)); // 12alert(Math.ceil(-11.4)); // -11alert(Math.ceil(-11.5)); // -11alert(Math.ceil(-11.8)); // -11alert(Math.floor(-11.4)); // -12alert(Math.floor(-11.5)); // -12alert(Math.floor(-11.8)); // -12alert(Math.round(-11.4)); // -11alert(Math.round(-11.5)); // -11alert(Math.round(-11.8)); // -12
4.random()方法
Math.random()方法返回一個(gè)隨機(jī)數(shù)(0≤r<1)。
例如,獲取一個(gè)1到10之間的整數(shù):
var num = Math.floor(Math.random() * 10 + 1);
5.其他方法
Math對(duì)象還提供了一些完成各種個(gè)中簡(jiǎn)單或復(fù)雜的計(jì)算。
ECMA-262 規(guī)定了這些方法,但是不同的實(shí)現(xiàn)可能精確度不同。
以上這篇深入理解JavaScript單體內(nèi)置對(duì)象就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注