<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          將偽數(shù)組轉(zhuǎn)換為數(shù)組的 N 種方案

          共 1861字,需瀏覽 4分鐘

           ·

          2020-11-05 12:24

          來源 |?http://www.fly63.com/article/detial/9787
          今天面試了一個(gè)人,居然不知道如何將偽數(shù)組轉(zhuǎn)換為數(shù)組?

          什么是偽數(shù)組?

          有 length 屬性,而且也是數(shù)值下標(biāo)的對象。
          不具備 Array.prototype 上的方法。

          常見偽數(shù)組

          argumentsdocument.getElementsByClassName
          $('div')

          偽數(shù)組轉(zhuǎn)換為數(shù)組

          輸出偽數(shù)組

          function fun(a,b,c = 1){    arr = arguments    console.log(        typeof arr,        Array.isArray(arr),        arr.length,        arr.slice,        arr,    )fun(3, 2)

          使用 Array.from (ES6+)(babel-polyfill)

          function fun(a,b,c = 1){    arr = Array.from(arguments)    console.log(        typeof arr,        Array.isArray(arr),        arr.length,        arr.slice,        arr,    )fun(3, 2)

          使用 ... 展開運(yùn)算符(ES6+)(babel)

          function fun(a,b,c = 1){    arr = [...arguments]    console.log(        typeof arr,        Array.isArray(arr),        arr.length,        arr.slice,        arr,    )fun(3, 2)

          使用 slice 和 call 的方案

          function fun(a,b,c = 1){    arr = Array.prototype.slice.call(arguments)    console.log(        typeof arr,        Array.isArray(arr),        arr.length,        arr.slice,        arr,    )    arr = Array.prototype.slice.apply(arguments)    console.log(        typeof arr,        Array.isArray(arr),        arr.length,        arr.slice,        arr,    )    arr = [].slice.call(arguments)    console.log(        typeof arr,        Array.isArray(arr),        arr.length,        arr.slice,        arr,    )    arr = [].slice.apply(arguments)    console.log(        typeof arr,        Array.isArray(arr),        arr.length,        arr.slice,        arr,    )}fun(3, 2)

          循環(huán)遍歷(兼容性無敵,樸素不)

          function fun(a,b,c = 1){    arr = [];    for(var i = 0,length = arguments.length; i < length; i++) {        arr.push(arguments[i]);    }    console.log(        typeof arr,        Array.isArray(arr),        arr.length,        arr.slice,        arr,    )}fun(3, 2)
          本文完~
          瀏覽 25
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  婬乱欧美一二三区 | 欧美亚洲福利 | 亚洲黄色视频免费 | 国产黄色视频免费在线观看 | 麻豆久久艹 |