Global對象
- 不屬于任何其他對象的屬性和方法艾蓝,最終都是Global對象的屬性和方法
- 所有在全局作用域中定義的屬性和函數(shù)遵班,都是Global 對象的屬性
- 諸如isNaN()季春、isFinite()馒胆、parseInt()以及parseFloat(),實際上全都是Global對象的方法
1讯沈、URI 編碼方法
- Global 對象的encodeURI()和encodeURIComponent()方法可以對URI(Uniform Resource Identifiers郁岩,通用資源標(biāo)識符)進(jìn)行編碼,以便發(fā)送給瀏覽器缺狠。
- 有效的URI 中不能包含某些字符问慎,例如空格。而這兩個URI 編碼方法就可以對URI進(jìn)行編碼挤茄,它們用特殊的UTF-8 編碼替換所有無效的字符如叼,從而讓瀏覽器能夠接受和理解。
- encodeURI()主要用于整個URI,不會對本身屬于URI的特殊字符進(jìn)行編碼驮樊,例如冒號薇正、正斜杠、問號和井字號囚衔。
- encodeURIComponent()主要用于對URI中的某一段,會對它發(fā)現(xiàn)的任何非標(biāo)準(zhǔn)字符進(jìn)行編碼
var uri = "http://www.wrox.com/illegal value.htm#start";
//"http://www.wrox.com/illegal%20value.htm#start"
alert(encodeURI(uri));
//"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start"
alert(encodeURIComponent(uri));
- 與encodeURI()和encodeURIComponent()方法對應(yīng)的兩個方法分別是decodeURI()和
decodeURIComponent()。 - decodeURI()只能對使用encodeURI()替換的字符進(jìn)行解碼
- decodeURIComponent()能夠解碼使用encodeURIComponent()編碼的所有字符雕沿,即它可以解碼任何特殊字符的編碼
var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start";
//http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start
alert(decodeURI(uri));
//http://www.wrox.com/illegal value.htm#start
alert(decodeURIComponent(uri));
2练湿、eval()方法
- eval()方法就像是一個完整的ECMAScript解析器,它只接受一個參數(shù)审轮,即要執(zhí)行的ECMAScript (或JavaScript)字符串
- 當(dāng)解析器發(fā)現(xiàn)代碼中調(diào)用eval()方法時肥哎,它會將傳入的參數(shù)當(dāng)作實際的ECMAScript 語句來解析辽俗,然后把執(zhí)行結(jié)果插入到原位置。
- 通過eval()執(zhí)行的代碼被認(rèn)為是包含該次調(diào)用的執(zhí)行環(huán)境的一部分篡诽,因此被執(zhí)行的代碼具有與該執(zhí)行環(huán)境相同的作用域鏈
var msg = "hello world";
eval("alert(msg)"); //"hello world"
eval("function sayHi() { alert('hi'); }");
sayHi();//"hi"
eval("var msg = 'hello world'; ");
alert(msg); //"hello world"
- 嚴(yán)格模式下崖飘,在外部訪問不到eval()中創(chuàng)建的任何變量或函數(shù),因此前面兩個例子都會導(dǎo)致錯誤杈女。同樣朱浴,在嚴(yán)格模式下,為eval 賦值也會導(dǎo)致錯誤:
"use strict";
eval = "hi"; //causes error
3达椰、Global對象的屬性
屬性 | 說明 | 屬性 | 說明 |
---|---|---|---|
undefined | 特殊值undefined | Date | 構(gòu)造函數(shù)Date |
NaN | 特殊值NaN | RegExp | 構(gòu)造函數(shù)RegExp |
Infinity | 特殊值Infinity | Error | 構(gòu)造函數(shù)Error |
Object | 構(gòu)造函數(shù)Object | EvalError | 構(gòu)造函數(shù)EvalError |
Array | 構(gòu)造函數(shù)Array | RangeError | 構(gòu)造函數(shù)RangeError |
Function | 構(gòu)造函數(shù)Function | ReferenceError | 構(gòu)造函數(shù)ReferenceError |
Boolean | 構(gòu)造函數(shù)Boolean | SyntaxError | 構(gòu)造函數(shù)SyntaxError |
String | 構(gòu)造函數(shù)String | TypeError | 構(gòu)造函數(shù)TypeError |
Number | 構(gòu)造函數(shù)Number | URIError | 構(gòu)造函數(shù)URIError |
- ECMAScript 5 明確禁止給undefined翰蠢、NaN和Infinity賦值,這樣做即使在非嚴(yán)格模式下也會導(dǎo)致錯誤啰劲。
4梁沧、window對象
- Web 瀏覽器都是將Global對象作為window對象的一部分加以實現(xiàn)的,在全局作用域中聲明的所有變量和函數(shù),都成為了window對象的屬性蝇裤。
var color = "red";
function sayColor(){
alert(window.color);
}
window.sayColor(); //"red"
Math對象
- 與我們在JavaScript 直接編寫的計算功能相比廷支,Math對象提供的計算功能執(zhí)行起來要快得多。
- Math 對象中還提供了輔助完成這些計算的屬性和方法栓辜。
1酥泞、Math對象的屬性
- Math 對象包含的屬性大都是數(shù)學(xué)計算中可能會用到的一些特殊值。下表列出了這些屬性啃憎。
屬 性 | 說 明 |
---|---|
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, 54, 32, 16);
alert(max); //54
var min = Math.min(3, 54, 32, 16);
alert(min); //3
- 要找到數(shù)組中的最大或最小值辛萍,可以像下面這樣使用apply()方法悯姊。
var values = [1, 2, 3, 4, 5, 6, 7, 8];
var max = Math.max.apply(Math, values);
3、舍入方法
- Math.ceil()執(zhí)行向上舍入贩毕,即它總是將數(shù)值向上舍入為最接近的整數(shù)悯许;
- Math.floor()執(zhí)行向下舍入,即它總是將數(shù)值向下舍入為最接近的整數(shù)辉阶;
- Math.round()執(zhí)行標(biāo)準(zhǔn)舍入先壕,即它總是將數(shù)值四舍五入為最接近的整數(shù);
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
4谆甜、random()方法
- Math.random()方法返回大于等于0 小于1 的一個隨機(jī)數(shù)垃僚。
- 套用下面的公式,就可以利用Math.random()從某個整數(shù)范圍內(nèi)隨機(jī)選擇一個值规辱。
值 = Math.floor(Math.random() * 可能值的總數(shù) + 第一個可能的值)
//選擇一個1到10 之間的數(shù)值
var num = Math.floor(Math.random() * 10 + 1);
//選擇一個2到10之間的數(shù)值
var num = Math.floor(Math.random() * 9 + 2);
- 多數(shù)情況下谆棺,其實都可以通過一個函數(shù)來計算可能值的總數(shù)和第一個可能的值。
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)的一個數(shù)值
var colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"];
var color = colors[selectFrom(0, colors.length-1)];
aler t(color); // 可能是數(shù)組中包含的任何一個字符串
5罕袋、其它方法
- Math 對象中還包含其他一些與完成各種簡單或復(fù)雜計算有關(guān)的方法
方 法 | 說 明 | 方 法 | 說 明 |
---|---|---|---|
Math.abs(num) | 返回num 的絕對值 | Math.asin(x) | 返回x 的反正弦值 |
Math.exp(num) | 返回Math.E 的num次冪 | Math.atan(x) | 返回x 的反正切值 |
Math.log(num) | 返回num 的自然對數(shù) | 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 的正切值 |
好好學(xué)習(xí)