<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] [https://wangdoc.com/javascript/](https://wangdoc.com/javascript/) ## typeof ``` typeof 123 // "number" typeof '123' // "string" typeof false // "boolean" typeof undefined typeof window // "object" typeof {} // "object" typeof [] // "object" typeof null // "object" 歷史原因 ``` ## 整數 ### null 與 undefined 區別 都可以在判斷的時候表示 fase; 只有在轉為數字時候 ``` Number(null) // 0 Number(undefined) // NaN ``` ### 判斷為聲明的js變量 ``` // 錯誤的寫法 if (v) { // ... } // 報錯: v is not defined // 正確的寫法 if (typeof v === "undefined") { // ... } ``` ### NaN NaN 是js整數類型的特殊值,保表示非數字 ``` 5 - 'x' // NaN Math.sqrt(-1) // NaN 0 / 0 // NaN typeof NaN // 'number' NaN + 32 // NaN ``` #### 判斷是否是 NaN ``` isNaN(NaN) // true isNaN(Number('Hello')) // true ``` 利用 NaN 不等于自身來判斷最有效 ``` function myIsNaN(value) { return value !== value; } ``` ### 轉為整數 ``` parseInt(' 8') // 8 parseInt('8a') // 8 parseInt('12**') // 12 parseInt('12.34') // 12 parseInt('15e2') // 15 parseInt('15px') // 15 parseInt('0x10') // 16 以十六進制轉換 parseInt('abc') // NaN parseInt('.3') // NaN ``` #### 轉為進制數 ``` parseInt('1000', 10) // 1000 parseInt('1000', 2) // 8 parseInt('1000', 6) // 216 parseInt('1000', 8) // 512 ``` ### 轉為浮點數 ``` parseFloat('3.14') // 3.14 parseFloat('314e-2') // 3.14 如果字符串符合科學計數法,則會進行相應的轉換。 ``` ## 字符串 ### Unicode 字符集 ``` var s = '\u0061'; //a var s = '\u00A9'; //? ``` js 只支持 UTF-16 支持不完整,兩字節的字符,不支持四字節的字符 ### Base64 轉碼 - `btoa()`:任意值轉為 Base64 編碼 - `atob()`:Base64 編碼轉為原來的值 ``` var string = 'Hello World!'; btoa(string) // "SGVsbG8gV29ybGQh" atob('SGVsbG8gV29ybGQh') // "Hello World!" ``` ## 函數 ### 函數表達式添加函數名 ``` var print = function demo(){ console.log(demo.name); //demo }; print(); ``` ### 函數的屬性和方法 #### name 值 ``` function f1() {} f1.name // "f1" var f2 = function () {}; f2.name // "f2" var f3 = function myName() {}; f3.name // 'myName' ``` 用途: 當函數通過參數傳入時,可以獲取函數名 ``` var myFunc = function () {}; function test(f) { console.log(f.name); } test(myFunc) // myFunc ``` #### length ``` function f(a, b) {} f.length // 2 ``` ### arguments 函數參數 對象,非數組 ``` var f = function (one) { console.log(arguments[0]);//1 console.log(arguments[1]);//2 console.log(arguments[2]);//3 } f(1,2,3) ``` ### 閉包 ``` function a(){ var n =12; function b(){ console.log(n); } return b; } var c = a(); c()//12 ``` 閉包的作用: * 可以讀取函數內部的變量, * 這些變量始終保持在內存中,即閉包可以使得它誕生環境一直存在 #### 變量始終保存在內存中 ``` function num(stat){ return function () { return stat++; } } var a = num(0); a();//0 a();//1 a();//2 ``` #### 初始化一個值 ``` function apiConnect(apiKey) { ? function get(route) { ? ? return fetch(`${route}?key=${apiKey}`); ? } ? function post(route, params) { ? ? return fetch(route, { ? ? ? method: 'POST', ? ? ? body: JSON.stringify(params), ? ? ? ? headers: { ? ? ? ? ? 'Authorization': `Bearer ${apiKey}` ? ? ? ? } ? ? ? }) ? } ? return { get, post } } const api = apiConnect('my-secret-key'); // No need to include the apiKey anymore api.get('http://www.example.com/get-endpoint'); api.post('http://www.example.com/post-endpoint', { name: 'Joe' }); ``` ## 數組 數組是一種對象 數組可放入任意類型的值 ``` var arr = [ {a: 1}, [1, 2, 3], function() {return true;} ]; arr[0] // Object {a: 1} arr[1] // [1, 2, 3] arr[2] // function (){return true;} ``` ### length - 如果賦值的長度小于原始長度,則會截取到賦值的長度 - 設置`length=0` ,清空數組 ## 錯誤處理機制 ### 原生錯誤類型 - SyntaxError 對象 : 解析代碼時發生的語法錯誤 - ReferenceError 對象 : 引用一個不存在的變量時發生的錯誤。 - RangeError 對象 : 一個值超出有效范圍時發生的錯誤 - TypeError 對象 : 變量或參數不是預期類型時發生的錯誤 - URIError 對象 : URI 相關函數的參數不正確時拋出的錯誤,主要涉及`encodeURI()、decodeURI()、encodeURIComponent()、decodeURIComponent()、escape()和unescape()`這六個函數 例子: ``` var err2 = new RangeError('出錯了,變量超出有效范圍!'); err3.name// "RangeError" err3.message // "出錯了,變量類型無效!" ``` ### 自定義錯誤 [推薦] ``` function UserError(message,code){ this.message = message || "默認錯誤信息"; this.name = "UserError"; this.code = code || undefined; } UserError.prototype = new UserError(); UserError.prototype.constructor = UserError try { throw new UserError("錯誤消息", 400) ;// 第二參數可不填 } catch (e) { console.log(e.message); console.log(e.code); } ``` ### Boolean,Number,String 類型轉換 ``` Boolean('a');//true Number('123');//123 String(true);//true ``` ## 運算符 ### 按位非運算符“~” #### ~value ``` if(arr.indexOf(ele) > -1){...} //易讀 if(~arr.indexOf(ele)){...} //簡潔 ``` #### ~~value `~~value`可以代替`parseInt(value)` ``` parseInt(-2.99) //-2 ~~(-2.99) //-2 ``` ### 強制轉布爾 `!!`
                  <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>

                              哎呀哎呀视频在线观看