Array.from vs Array.prototype.map

前端 未结 4 1025
耶瑟儿~
耶瑟儿~ 2021-02-18 15:27

So what is the difference between this two functions?

They both create new Array object. Only difference I found so far is that Array.from supp

4条回答
  •  孤街浪徒
    2021-02-18 15:56

    Making Array.prototype the prototype object for every single array-like "Class" in JS (more importantly, in DOM, where most of the 'array-like' objects live) would be a potential mistake.

    What would a .reduce( ) on a list of HTML elements/attributes look like?

    Array.from is the official version of [].slice.call(arrayLike); with the added benefit of not having to create an unused array, just to create an array.

    So really, Array.from can be polyfilled with function (arrLike) { return [].slice.call(arrLike); }, and minus native-implementation speed/memory improvements, it's the same result.

    This has little to do with map|reduce|filter|some|every|find, which are the keys to living a long and happy life, without the need of micromanaging loops to get things done.

提交回复
热议问题