<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 單體內置對象(Global、Math) 內置對象也就是我們不必顯示地區實例化,直接就可以使用了,因為它們已經實例化了。 **1、Global 對象** Global對象是一個全局對象。所有在全局作用域中定義的屬性和函數,都是Global對象的屬性,比如:isNaN()、isFinite()、parseInt()以及parseFloat(),實際上都是Global對象的方法,它還包括了其他一些方法: **1.1 Global的方法** **(1)URI編碼方法** Global對象的`encodeURI()`和`encodeURIComponent()`方法可以對URI(Uniform Resource Identifiers,通用資源標識符)進行編碼,以便發送給服務器(在GET 請求中很重要的方法)。 有效的URI中不能包含某些字符,比如空格。 `encodeURI()`主要用于整個URI;`encodeURIComponent()`主要用于對URI中的某一段(比如:illegal value.html這一段) `encodeURI()`和`encodeURIComponent()`的區別: - `encodeURI()`不會對本身屬于URI的特殊字符進行編碼(比如冒號、正斜杠、問號和井字號);但`encodeURIComponent()`則會對它發現的任何非標準字符進行編碼。 ``` var uri = 'http://www.example.com/illegal value.html#start'; console.log(encodeURI(uri)); // http://www.example.com/illegal%20value.html#start console.log(encodeURIComponent(uri)); // http%3A%2F%2Fwww.example.com%2Fillegal%20value.html%23start ``` 從上面的例子的結果,我們也可以看出兩者的區別,encodeURI()只對空格進行了編碼,而encodeURIComponent()使用對應的編碼替換了所有非標準的字符。 與`encodeURI()`和`encodeURIComponent()`方法對應的是`decodeURI()`和`decodeURIComponent()`。 decodeURI()只能對使用encodeURI()替換的字符進行解碼;decodeURIComponent()能夠解碼使用encodeURIComponent()編碼的所有字符。 ``` console.log(decodeURI('http://www.example.com/illegal%20value.html#start')); // http://www.example.com/illegal value.html#start console.log(decodeURIComponent('http%3A%2F%2Fwww.example.com%2Fillegal%20value.html%23start')); // http://www.example.com/illegal value.html#start ``` **(2)eval() 方法** eval()方法類似一個完整的ECMAScript解析器,它接受一個參數,既要執行的JavaScript字符串。 ``` eval("alert('hi')"); //等價于 alert('hi'); ``` 當解析器發現代碼中調用eval()方法時,它會將傳入的參數當作實際的ECMAScript語句來解析,然后把執行結果插入到原位置。 通過eval()執行的代碼被認為是包含該次調用的執行環境的一部分,因此被執行的代碼具有與該執行環境相同的作用域鏈。 ``` var name = 'tg'; eval("console.log(name)"); // "tg" eval("function test() { console.log(1); }"); test(); // 1 ``` 注意:在eval()創建的任何變量或函數都不會被提升。而且在嚴格模式下,在外部是訪問不到eval()中創建的任何變量或函數。 **1.2 Global對象的屬性** undefined、NaN、Infinity、Object、Array、Function、Boolean、String、Number、Date、RegExp、Error、EvalError、RangeError、ReferenceError、SyntaxError、TypeError、URIError **1.3 window對象** ECMAScript并沒有指出如何直接訪問Global對象,但Web瀏覽器都是講這個全局對象作為window對象的一部分加以實現的。因此,在全局作用域總聲明的的所有變量和函數,都會成為window對象的屬性。 **2、Math 對象** Math對象中保存了數學公式和信息。 **2.1 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的平方根(即2的平方根的倒數) Math.SQRT2 2的平方根 ``` **2.2 Math對象的方法** **(1)Math.min()和Math.max()** `Math.min()`和`Math.max()`分別用于確定一組數值中的最小值和最大值,這兩個方法都可以接收任意個數值參數。 ``` console.log(Math.max(3, 10, 2, 100)); // 100 console.log(Math.min(3, 10, 2, 100)); // 2 ``` 如果要找到數組中的最大或最小值,可以這樣: ``` var arr = [3, 10, 2, 100]; var max = Math.max.apply(Math, arr); console.log(max); // 100 ``` apply()方法是用來改變一個函數內的this指向,第一個參數就是要this指向的對象,第二個參數是一個數組。 **(2)舍入方法** `Math.ceil()`執行向上舍入,即它總是取最接近數值且大于數值的整數 ``` console.log(Math.ceil(1.4)); // 2 console.log(Math.ceil(1.5)); // 2 ``` `Math.floor()`執行向下舍入,即它總是取最近數值且小于數值的整數。 ``` console.log(Math.floor(1.4)); // 1 console.log(Math.floor(1.5)); // 1 ``` `Math.round()`執行標準舍入,即它總是將數值四舍五入為最接近的整數。 ``` console.log(Math.round(1.4)); // 1 console.log(Math.round(1.5)); // 2 ``` **(3)random() 方法** `Math.random()`方法返回介于0和1之間的一個隨機數(不包括0和1)。 ``` console.log(Math.random()); // 介于0~1之間的值 ``` 如果你要取其他的隨機數,可以這樣: ``` var rand = Math.random() * 10; // 介于0~10之間的值 var iRand = Math.floor(rand); // 介于0~10之間的整數 ``` 總結了一個方法: ``` function getRandom(max, min) { min = arguments[1] || 0; return Math.floor(Math.random() * (max - min + 1) + min); }; ``` getRandom()方法接受兩個參數:應該返回的最小值和最大值。 **(4)其他方法** 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的反正弦值 Matn.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>

                              哎呀哎呀视频在线观看