<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                #第十二章內置對象 講師:_無腦碼農(張磊)_ 博客:http://www.h5code.com 課程內容: [TOC] * * * * * >ECMA-262 對內置對象的定義是:“由ECMAScript實現提供的、不依賴宿主環境的對象,這些對象在ECMAScript程序執行之前就已經存在了。”意思就是說,開發人員不必顯示地實例化內置對象;因為它們已經實例化了。ECMA-262 只定義了兩個內置對象:Global 和Math。 ###1.>Global 對象 >Global(全局)對象是ECMAScript中一個特別的對象,因為這個對象是不存在的。在ECMAScript中不屬于任何其他對象的屬性和方法,都屬于它的屬性和方法。所以,事實上,并不存在全局變量和全局函數;所有在全局作用域定義的變量和函數,都是Global對象的屬性和方法。 *PS:因為ECMAScript沒有定義怎么調用Global對象,所以,Global.屬性或者Global.方法()都是無效的。(Web瀏覽器將Global作為window 對象的一部分加以實現)* >Global 對象有一些內置的屬性和方法: 1.URI 編碼方法 >URI 編碼可以對鏈接進行編碼,以便發送給瀏覽器。它們采用特殊的UTF-8 編碼替換所有無效字符,從而讓瀏覽器能夠接受和理解。encodeURI()不會對本身屬于URI的特殊字符進行編碼,例如冒號、正斜杠、問號和#號;而encodeURIComponent()則會對它發現的任何非標準字符進行編碼 ~~~ var box = '//Zhang 張'; alert(encodeURI(box)); //只編碼了中文 var box = '//Zhang 張'; alert(encodeURIComponent(box)); //特殊字符和中文編碼了 ~~~ *PS:因為encodeURIComponent()編碼比encodeURI()編碼來的更加徹底,一般來說encodeURIComponent()使用頻率要高一些。使用了URI 編碼過后,還可以進行解碼,通過decodeURI()和decodeURIComponent()來進行解碼* ~~~ var box = '//Zhang 張'; alert(decodeURI(encodeURI(box))); //還原 var box = '//Zhang 張'; alert(decodeURIComponent(encodeURIComponent(box))); //還原 ~~~ *PS:URI 方法如上所述的四種,用于代替已經被ECMA-262 第3 版廢棄的escape()和unescape()方法。URI方法能夠編碼所有的Unicode 字符,而原來的只能正確地編碼ASCII字符。所以建議不要再使用escape()和unescape()方法。* 2.eval()方法 >eval()方法主要擔當一個字符串解析器的作用,他只接受一個參數,而這個參數就是要執行的JavaScript 代碼的字符串。 ~~~ eval('var box = 100'); //解析了字符串代碼 alert(box); eval('alert(100)'); //同上 eval('function box() {return 123}'); //函數也可以 alert(box()); ~~~ >eval()方法的功能非常強大,但也非常危險。因此使用的時候必須極為謹慎。特別是在用戶輸入數據的情況下,非常有可能導致程序的安全性,比如代碼注入等等。 3.Global 對象屬性 >Global 對象包含了一些屬性:undefined、NaN、Object、Array、Function 等等。 `alert(Array); //返回構造函數` 4.window 對象 >之前已經說明,Global沒有辦法直接訪問,而Web瀏覽器可以使用window 對象來實現一全局訪問。 `alert(window.Array); //同上` ###2.>Math 對象 >ECMAScript 還為保存數學公式和信息提供了一個對象,即Math 對象。與我們在JavaScript直接編寫計算功能相比,Math對象提供的計算功能執行起來要快得多。 1.Math 對象的屬性 >Math 對象包含的屬性大都是數學計算中可能會用到的一些特殊值。 | 屬性|說明 | | -- | -- | | Math.E| 自然對數的底數,即常量e 的值 | | Math.LN10| 10 的自然對數 | | Math.LN2| 2 的自然對數 | | Math.LOG2E| 以2 為底e 的對數 | | Math.LOG10E| 以10 為底e 的對數 | | Math.PI| Π的值 | | Math.SQRT1_2| 1/2 的平方根 | | Math.SQRT2| 2 的平方根 | ~~~ alert(Math.E); // alert(Math.LN10); alert(Math.LN2); alert(Math.LOG2E); alert(Math.LOG10E); alert(Math.PI); alert(Math.SQRT1_2); alert(Math.SQRT2); // ~~~ 2.min()和max()方法 >Math.min()用于確定一組數值中的最小值。Math.max()用于確定一組數值中的最大值。 ~~~ alert(Math.min(2,4,3,6,3,8,0,1,3)); //最小值 alert(Math.max(4,7,8,3,1,9,6,0,3,2)); //最大值 ~~~ 3.舍入方法 >Math.ceil()執行向上舍入,即它總是將數值向上舍入為最接近的整數; Math.floor()執行向下舍入,即它總是將數值向下舍入為最接近的整數; Math.round()執行標準舍入,即它總是將數值四舍五入為最接近的整數; ~~~ 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)); //26 alert(Math.round(25.1)); //25 ~~~ 4.random()方法 >Math.random()方法返回介于0 到1 之間一個隨機數,不包括0 和1。如果想大于這個范圍的話,可以套用一下公式: >值= Math.floor(Math.random() * 總數+ 第一個值 ~~~ alert(Math.floor(Math.random()*10+1));//隨機產生1-10之間的任意數 for (var i = 0; i<10;i ++) { document.write(Math.floor(Math.random() * 10 + 5)); //5-14 之間的任意數 document.write('<br />'); } ~~~ >為了更加方便的傳遞想要范圍,可以寫成函數: ~~~ function selectFrom(lower, upper) { var sum = upper - lower + 1; //總數-第一個數+1 return Math.floor(Math.random() * sum + lower); } for (var i=0 ;i<10;i++) { document.write(selectFrom(5,10)); //直接傳遞范圍即可 document.write('<br />'); } ~~~ 5.其他方法 | 方法|說明 | | -- | -- | | Math.abs(num)| 返回num 的絕對值 | | Math.exp(num)| 返回Math.E 的num 次冪| | Math.log(num)| 返回num 的自然對數| | 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 的正切值 |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看