學(xué)習(xí)要點(diǎn):
1. Global對(duì)象
2. Math對(duì)象
一、Global對(duì)象
Global(全局)對(duì)象是ECMAScript中的一個(gè)特別方法,因?yàn)檫@個(gè)對(duì)象是不存在的。在ECMAScript中不屬于任何其他對(duì)象的屬性和方法,都屬于它的屬性和方法。所以,事實(shí)上,并不存在全局變量和全局函數(shù);所有在全局作用域定義的變量和函數(shù),都是Global對(duì)象的屬性和方法。
因?yàn)镋CMAScript沒(méi)有定義如何調(diào)用Global對(duì)象,所以,Global屬性或者Global方法都是無(wú)效的(web瀏覽器將Global作為window對(duì)象的一部分加以實(shí)現(xiàn))。
Global對(duì)象有一些內(nèi)置的屬性和方法:
1.URI編碼方法
URI編碼可以對(duì)鏈接進(jìn)行編碼,以便發(fā)送給瀏覽器。采用了特殊的UTF-8編碼替換所有無(wú)效字符,從而讓瀏覽器能接受和理解。
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)行編碼。
const str = '小龍 lee';
console.log(encodeURI('//' + str));
// //%E5%B0%8F%E9%BE%99%20lee 只編譯了中文
console.log(encodeURIComponent('//' + str));
// %2F%2F%E5%B0%8F%E9%BE%99%20lee 編譯了中文和符號(hào)
console.log(decodeURI('%E5%B0%8F%E9%BE%99%20lee')); // //小龍 lee
console.log(decodeURIComponent('%2F%2F%E5%B0%8F%E9%BE%99%20lee'));// //小龍 lee
// 解碼一定要相對(duì)應(yīng)
因?yàn)閑ncodeURIComponent()編碼會(huì)比encodeURI()編碼更徹底,所以前者使用的更多一些。
2. eval方法
擔(dān)當(dāng)一個(gè)字符串解析器的作用,只接受一個(gè)參數(shù),這個(gè)參數(shù)就是要執(zhí)行的js代碼的字符串。
eval('alert(10)'); // 10
這個(gè)方法容易導(dǎo)致程序安全性問(wèn)題,比如代碼注入等。不使用。
3. Global對(duì)象屬性
Global對(duì)象包含了一些屬性:undefined、NaN、Object、Array、Function等。
console.log(Array);
// ? Array() { [native code] } 返回構(gòu)造函數(shù)
4. window對(duì)象
5. Global無(wú)法直接訪問(wèn),而web瀏覽器可以使用window對(duì)象來(lái)實(shí)現(xiàn)全局訪問(wèn)
console.log(window.Array);
// ? Array() { [native code] } 返回構(gòu)造函數(shù)
二、math對(duì)象
ECMAScript為保存數(shù)學(xué)公式和信息提供了一個(gè)對(duì)象,即Math對(duì)象。與我們?cè)贘avaScript直接編寫(xiě)數(shù)學(xué)計(jì)算功能相比,Math對(duì)象提供的計(jì)算功能執(zhí)行起來(lái)要快得多。
1. Math對(duì)象的屬性
Math對(duì)象包含的屬性大都是數(shù)學(xué)計(jì)算中可能用到的一些特殊值。
| 屬性 | 說(shuō)明 |
|---|---|
| Math.E | 自然對(duì)數(shù)的底數(shù),即常量 |
| Math.LN10 | 10的自然對(duì)數(shù) |
| Math.LN2 | 2的自然對(duì)數(shù) |
| Math.LOG2E | 以2為底e的對(duì)數(shù) |
| Math.LOG10E | 以10為底e的對(duì)數(shù) |
| Math.PI | π的值 |
| Math.SQRT1_2 | 1/2的平方根 |
| Math.SQRT2 | 2的平方根 |
console.log(Math.E);
// 2.718281828459045
console.log(Math.PI);
// 3.141592653589793
//...
2. min()和max()方法
console.log(Math.min(55, 66, 77, 55, 44, 22, 1)); // 1
console.log(Math.max(55, 66, 77, 55, 44, 22, 1)); // 77
3. 舍入方法
Math.ceil()執(zhí)行向上舍入,即總是將值向上舍入為接近的整數(shù)
Math.floor()執(zhí)行向下舍入,即總是將值向下舍入為接近的整數(shù)
Math.round()執(zhí)行標(biāo)準(zhǔn)舍入,即總是將值四舍五入為接近的整數(shù)
var num1 = 3.1415926;
var num2 = 3.5;
var num3 = 3.9;
console.log(Math.ceil(num1)); // 4
console.log(Math.ceil(num2)); // 4
console.log(Math.ceil(num3)); // 4
console.log(Math.floor(num1)); // 3
console.log(Math.floor(num2)); // 3
console.log(Math.floor(num3)); // 3
console.log(Math.round(num1)); // 3
console.log(Math.round(num2)); // 4
console.log(Math.round(num3)); // 4
4. random()方法
Math.random方法可以返回介于0-1之間的一個(gè)隨機(jī)數(shù),不包括0和1。如果想大于這個(gè)范圍的話,可以套用以下公式:
值 = Math.floor(Math.random() * 總數(shù) + 第一個(gè)值);
console.log(Math.floor(Math.random * 10 + 1));
//返回1-10的隨機(jī)數(shù) 包含1和10
console.log(Math.floor(Math.random() * 6 + 5));
//返回5-10的隨機(jī)數(shù) 包含5和10
//封裝為函數(shù)
function numRandom(start, end) {
var total = end - start + 1;
return Math.floor(Math.random() * total + start);
}
for (var i = 0; i < 10; i++) {
document.write(numRandom(5, 10));
document.write('<br>');
}
5. 其他方法:
| 方法 | 描述 |
|---|---|
| abs(x) | 返回?cái)?shù)字的絕對(duì)值 |
| acos(x) | 返回?cái)?shù)字的反余弦值 |
| asin(x) | 返回?cái)?shù)字的反正弦值 |
| atan(x) | 返回位于-PI/2 和 PI/2 的反正切值 |
| atan2(y,x) | 返回(x,y)位于 -PI 到 PI 之間的角度 |
| ceil(x) | 返回 x 四舍五入后的最大整數(shù) |
| cos(x) | 返回一個(gè)數(shù)字的余弦值 |
| exp(x) | 返回 E^x 值 |
| log(x) | 返回底數(shù)為E的自然對(duì)數(shù) |
| pow(x,y) | 返回 y^x 的值 |
| sin(x) | 返回?cái)?shù)字的正弦值 |
| sqrt(x) | 返回?cái)?shù)字的平方根 |
| tan(x) | 返回一個(gè)角度的正切值 |
| toSource() | 顯示對(duì)象的源代碼 |
| valueOf() | 返回?cái)?shù)學(xué)對(duì)象的原始值 |