SukkaW / DisqusJS

:speech_balloon: Render Disqus comments in Mainland China using Disqus API
https://disqusjs.skk.moe
MIT License
646 stars 56 forks source link

Use Array.prototype.slice.call instead of Array.from #52

Closed renzibei closed 4 years ago

renzibei commented 4 years ago

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.

renzibei commented 4 years ago

Hey, what do you think of this? @SukkaW

SukkaW commented 4 years ago

@renzibei Maybe we should do it in polyfill way:

https://github.com/SukkaW/DisqusJS/blob/d13b4d60adee4c6111c8075b75689e87df41beb2/src/disqus.js#L31-L45

renzibei commented 4 years ago

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

SukkaW commented 4 years ago

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);
}
renzibei commented 4 years ago

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.