<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] >[success] # 數組的解構賦值 <br/> >[success] ## 基本用法 ES6 允許按照一定模式,從數組和對象中提取值,對變量進行賦值,這被稱為解構。 以前,為變量賦值,只能直接指定值。 ~~~ let a = 1; let b = 2; let c = 3; ~~~ ES6 允許寫成下面這樣,這種寫法為匹配模式,只要`=`兩邊的模式相同,左邊的變量就會被賦予對應的值。 ~~~ let [a, b, c] = [1, 2, 3]; console.log( a ) // 1 console.log( b ) // 2 console.log( c ) // 3 ~~~ <br/> >[success] ## 嵌套數組 第一種寫法,按照數組的格式來嵌套就可以取出來對應的數據 ~~~ let [foo, [[bar], baz]] = [1, [[2], 3]]; console.log( foo ) // 1 console.log( bar ) // 2 console.log( baz ) // 3 ~~~ 第二種寫法,即使左側的一些值未定義逗號分隔還在,也是可以的 ~~~ let [ , , third] = ["foo", "bar", "baz"]; console.log( third ) // "baz" ~~~ 第三種寫法,配合擴展運算符 ~~~ let [head, ...tail] = [1, 2, 3, 4]; console.log( head ) // "1" console.log( tail ) // [2, 3, 4] ~~~ <br/> >[danger] ## 錯誤寫法 如果解構不成功,變量的值就等于`undefined`。 ~~~ let [x, y, ...z] = ['a']; console.log( x ) // "a" console.log( y ) // undefined console.log( z ) // [] ~~~ 以下兩種情況都屬于解構不成功,foo的值都會等于undefined。 另一種情況是不完全解構,即等號左邊的模式,只匹配一部分的等號右邊的數組。這種情況下,解構依然可以成功。 ~~~ let [foo] = []; let [bar, foo] = [1]; ~~~ 以上兩種情況都屬于解構不成功,foo的值都會等于undefined。 ~~~ let [x, y] = [1, 2, 3]; console.log( x ) // 1 console.log( y ) // 2 let [a, [b], d] = [1, [2, 3], 4]; console.log( a ) // 1 console.log( b ) // 2 console.log( d ) // 4 ~~~ 上面兩個例子,都屬于不完全解構,但是可以成功。 ~~~ // 報錯 let [foo] = 1; let [foo] = false; let [foo] = NaN; let [foo] = undefined; let [foo] = null; let [foo] = {}; ~~~ 上面的語句都會報錯,因為等號右邊的值,要么轉為對象以后不具備 Iterator 接口(前五個表達式),要么本身就不具備 Iterator 接口(最后一個表達式)。 <br/> >[success] ## 默認值 解構賦值允許指定默認值,可以賦值默認值的位置是等號的左面(let [a='3'] = []) ~~~ let [foo = true] = []; console.log( foo ) // true let [x, y = 'b'] = ['a']; // x='a', y='b' let [x, y = 'b'] = ['a', undefined]; // x='a', y='b' ~~~ 注意,ES6 內部使用嚴格相等運算符(===),判斷一個位置是否有值。所以,如果一個數組成員不嚴格等于undefined,默認值是不會生效的。 ~~~ let [x = 1] = [undefined]; console.log( x ) // 1 let [x = 1] = [null]; console.log( x ) // null ~~~ ~~~ function f() { console.log('aaa'); } let [x = f()] = [1]; ~~~ 上面代碼中,因為x能取到值,所以函數f根本不會執行。上面的代碼其實等價于下面的代碼。 ~~~ let x; if ([1][0] === undefined) { x = f(); } else { x = [1][0]; } ~~~ 默認值可以引用解構賦值的其他變量,但該變量必須已經聲明。 ~~~ let [x = 1, y = x] = []; // x=1; y=1 let [x = 1, y = x] = [2]; // x=2; y=2 let [x = 1, y = x] = [1, 2]; // x=1; y=2 let [x = y, y = 1] = []; // ReferenceError ~~~ 上面最后一個表達式之所以會報錯,是因為x用到默認值y時,y還沒有聲明。
                  <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>

                              哎呀哎呀视频在线观看