Closed renzibei closed 4 years ago
Hey, what do you think of this? @SukkaW
@renzibei Maybe we should do it in polyfill way:
@SukkaW Hi SukkaW ! The MDN version of polyfill of Array.from is a bit long. The difference between Array.prototype.slice.call, Array.from and spread syntax in this area is shown in the table below.
Array.prototype.slice.call | Array.from | Spread Syntax | |
---|---|---|---|
Array-like objects with a length property and indexed elements | Yes | Yes | No |
Iterable objects | No | Yes | Yes |
If we want to handle HTMLCollection here, all of them should be fine for modern browsers (Symbol.iterator is set true for HTMLCollection in recent years). And I think Array.prototype.slice.call is supported most widely.
Of course, if you agree to include a longer ployfill in the code, I have no opinion. It's up to you.
Of course, if you agree to include a longer ployfill in the code, I have no opinion. It's up to you.
A simple htmlCollectionToArray
function will do:
function htmlCollectionToArray(...args) {
const fn = Array.from || Array.prototype.slice.call;
return fn.apply(this, args);
}
Of course, if you agree to include a longer ployfill in the code, I have no opinion. It's up to you.
A simple
htmlCollectionToArray
function will do:function htmlCollectionToArray(...args) { const fn = Array.from || Array.prototype.slice.call; return fn.apply(this, args); }
Yes this function can be a ployfill when only handling the HTMLCollection
. But when we only have to consider HTMLCollection
, I don't know what's the advantage of Array.from
over Array.prototype.slice.call
so that we should create a function to switch to it when using modern browsers.
Array.from is defined in ES6 (ECMA-262), which has not been supported by all the browsers (IE). So use Array.prototype.slice.call instead.