溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

單體內(nèi)置對(duì)象

發(fā)布時(shí)間:2020-07-09 10:01:10 來(lái)源:網(wǎng)絡(luò) 閱讀:287 作者:吳金瑞 欄目:網(wǎng)絡(luò)安全

    

定義:由ECMAScript實(shí)現(xiàn)提供的,不依賴與宿主環(huán)境的對(duì)象,這些對(duì)象在ECMASript程序執(zhí)行之前就已經(jīng)存在了。

意思就是說(shuō),開(kāi)發(fā)人員不必顯示的實(shí)例化內(nèi)置對(duì)象,因?yàn)樗麄円呀?jīng)實(shí)例化了。例如Object、Array和String。ECMA-262還定義了兩個(gè)單體內(nèi)置對(duì)象:Global和Math。

 

Global對(duì)象

 


 

在某種意義上他是一個(gè)兜底的的對(duì)象,所有在全局作用域中定義的屬性和方法都是Global的屬性。諸如isNaN(),isFinite(),parseInt(),parseFloat()。

1、URI編碼方法

 

encodeURI()主要用于整個(gè)URI

encodeURIComponent()主要對(duì)URI的某一段

 

主要區(qū)別:encodeURI()不會(huì)對(duì)屬于URI的特殊字符進(jìn)行編碼,例如冒號(hào)、正斜杠。encodeURIComponent()則會(huì)對(duì)所有非標(biāo)準(zhǔn)字符進(jìn)行編碼

 

1          var uri = "http://www.wrox.com/illegal value.html#start";2          console.log(encodeURI(uri)); //http://www.wrox.com/illegal%20value.html#start3          console.log(encodeURIComponent(uri)); 
4          // http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.html%23start

 

與encodeURI()和encodeURIComponent()對(duì)應(yīng)的兩個(gè)方法是decodeURI()和decodeURIComponent()

 

1          var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.html%23start";2          console.log(decodeURI(uri)); //http%3A%2F%2Fwww.wrox.com%2Fillegal value.html%23start3          console.log(decodeURIComponent(uri)); //http://www.wrox.com/illegal value.html#start

 

2、eval()方法

 eval()方法就像是一個(gè)完整的ECMAScript解析器,它只接受一個(gè)參數(shù),即要執(zhí)行的ECMAScript字符串

 

1         eval("alert('hi')");

 

相當(dāng)于執(zhí)行下面代碼

 

1         alert("hi");

 

 

它會(huì)將傳入的參數(shù)當(dāng)做實(shí)際的ECMAScript語(yǔ)句來(lái)解析,然后把執(zhí)行結(jié)果插入到原來(lái)位置。通過(guò)eval()執(zhí)行的代碼被認(rèn)為是包含該次調(diào)用的執(zhí)行環(huán)境的一部分,所以通過(guò)eval()
執(zhí)行的代碼可以引用在包含環(huán)境中定義的變量。

 

1          var msg = "hello word";2          eval(alert(msg));3 4          eval("function sayHi(){alert('hi')}");5          sayHi();

 

 

在eval()中創(chuàng)建的任何變量和函數(shù)都不會(huì)被提升,因?yàn)樵诮馕龃a的時(shí)候,它們被包含在一個(gè)字符串中,它們只在eval()執(zhí)行的時(shí)候創(chuàng)建。并且在嚴(yán)格模式下,在外部訪問(wèn)不到eval()中創(chuàng)建的任何變量和函數(shù)

 

1         console.log(sayHi()); //sayHi is not defined2         eval("function sayHi(){alert('hi')}");

 

3、Global對(duì)象的屬性

 

屬  性說(shuō)  明屬  性說(shuō)  明
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ù)ArrayRangeError 構(gòu)造函數(shù)RangeError
Function  構(gòu)造函數(shù)Function ReferenceError構(gòu)造函數(shù)ReferenceError
Boolean   構(gòu)造函數(shù)BooleanSyntaxError構(gòu)造函數(shù)SyntaxError
String 構(gòu)造函數(shù)String TypeError 構(gòu)造函數(shù)TypeError
Number  構(gòu)造函數(shù)NumberURIError 構(gòu)造函數(shù)URIError

 

4、Window對(duì)象

 

在Web瀏覽器中都是將這個(gè)全局對(duì)象作為window對(duì)象的一部分加以實(shí)現(xiàn)的,因此,在全局作用域中定義的所有變量和函數(shù)都成為了window對(duì)象的屬性。

 

1          var color = "red";2          function sayColor(){3              console.log(window.color);4          }5          window.sayColor(); //"red"

 

另一種取的Global對(duì)象的方法是使用以下代碼:

 

1          var global = function(){2              return this;3          }();

 

Math對(duì)象

 


 

ECMAScript還為保存數(shù)學(xué)公式和信息提供了一個(gè)公共位置,即Math對(duì)象,與我們?cè)趈avascript中編寫的計(jì)算功能相比,Math對(duì)象提供的計(jì)算功能執(zhí)行起來(lái)要快的多

 

 

1、Math對(duì)象的屬性

 

屬  性 說(shuō)  明
Math.E 自然對(duì)數(shù)的底數(shù),即常量的e的值
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的平方根(即2的平方根的倒數(shù))
Math.SQRT2 2的平方根

 

2、min()和max()方法

 

1         var max = Math.max(3,56,98,32);2         console.log(max); //983 4         var min  = Math.min(3,56,98,32);5         console.log(min); //3

 

要找到數(shù)組中最大值或者最小值,可以像下面這樣使用apply()方法

 

1         var newArray = [1,2,3,6,5,4,8,9];2         var max = Math.max.apply(Math,newArray);3         console.log(max);

 

3、舍入方法

 

Math.ceil()執(zhí)行向上舍入
Math.floor()執(zhí)行向下舍入
Math.round()執(zhí)行標(biāo)準(zhǔn)舍入

 

單體內(nèi)置對(duì)象

 1         alert(Math.ceil(25.9));//26 2         alert(Math.ceil(25.5));//26 3         alert(Math.ceil(25.1));//26 4  5         alert(Math.floor(25.9));//25 6         alert(Math.floor(25.5));//25 7         alert(Math.floor(25.1));//25 8  9         alert(Math.round(25.9));//2610         alert(Math.round(25.5));//2611         alert(Math.round(25.1));//25

單體內(nèi)置對(duì)象


4、random()方法

 

返回大于等于0小于1的一個(gè)隨機(jī)數(shù)

值 = Math.floor(Math.random() * 可能值的總數(shù) + 第一個(gè)可能的值)

選擇一個(gè)2到10之間的數(shù)值

 

1         var num = Math.floor(Math.random() * 9 + 2);2         console.log(num);

 

多數(shù)情況下,其實(shí)都可以通過(guò)一個(gè)函數(shù)來(lái)計(jì)算可能值的總數(shù)和第一個(gè)可能的值,例如

 

單體內(nèi)置對(duì)象

 1         function selectForm(lowerValue,upperValue){ 2             var choices = upperValue - lowerValue + 1; 3             return Math.floor(Math.random() * choices + lowerValue); 4         } 5         var num = selectForm(2,10); 6         console.log(num); 7  8         //利用這個(gè)函數(shù)可以方便的從數(shù)組中隨機(jī)的取出一項(xiàng) 9 10         var colors = ["red","green","blue","yellow","black","purple","brown"];11         var color = colors[selectForm(0,colors.length-1)];12         console.log(color);

單體內(nèi)置對(duì)象

 

5、其他方法

 

Math對(duì)象中還包含其他一些與完成各種簡(jiǎn)單或復(fù)雜計(jì)算有關(guān)的方法。見(jiàn)下表:

 

方  法  說(shuō)  明方  法 說(shuō)  明
Math.abs(num) 返回num的絕對(duì)值 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的正切值


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI