Rick Waldron (2013-12-28T19:24:29.000Z)
domenic at domenicdenicola.com (2014-01-08T22:18:28.768Z)
On Sat, Dec 28, 2013 at 11:37 AM, David Bruant <bruant.d at gmail.com> wrote: > In ES6 and above, why would one create such an object? What's a good use > case? > My understanding of the current consensus is that an arraylike without > @@iterator wouldn't work for for-of loops nor spread. Why not just create > an array? jQuery and Zepto want to subclass `Array` (one creates arraylike, > the other does subclass setting `__proto__`). It wasn't possible in ES5, but > is in ES6 with classes (and the super+@@create infrastructure). jQuery is a bad example to use in this case, because it will _never_ include features that can't be made to work consistently across all platforms that are supported. This includes jQuery 2.x, which has no less and no more features or capabilities than jQuery 1.x (and never will). > I feel that all the cases that justified arraylikes in the past have much > better alternatives in ES6. > My little experience building a Firefox addon even suggests that sets > replace arrays in most situations as most of what I do with arrays is > .push, for-of and .map/filter/reduce (by the way, Set.prototype needs these > too, but another topic for another time). Realistically, this is a minority use case. The most common use case is a web-based application that often must work on platforms as old as IE8, and that shouldn't _limit_ the progress and evolution in ES6 features. > If these objects have a good reason to exist in an ES6 and above world, I > agree, that's a good point. But is there a use case justifying their > existence? In the ES6 world, there will be userland and platform objects that can't be upgraded to real iterables, eg. application code that must behave correctly on platforms with and without @@iterator (ie. browsers that have large market share but don't auto-update). Array.from can be shimmed such that code like the following will work correctly on both older ES3-5 (Array.prototype methods shimmed for ES3) or ES6 platforms: ```js if (typeof Array.from === "undefined") { ...shim it. } function f() { // Works correctly on browsers that have implemented @@iterator on arguments objects // as well as those that have not. return Array.from(arguments).map(...); } // Or... // Works correctly on browsers that have implemented @@iterator on NodeList objects // as well as those that have not. Array.from(qSA(".selector")).forEach(...); ``` `for-of` won't even exist on these older platforms, but for code that only needs to run on ES6 platforms, `Array.from` provides a "spread" or "make iterable" mechanism for those objects that _can't_ be upgraded (for any reason, as noted above): ```js // Turn a jQuery object into an iterable: Array.from(jQuery(".selector")); ``` (This can't be done with spread) Array.from bridges the gap between ES3/5 and ES6 while remaining useful in an ES6 and post ES6 world by providing an "assimilation" path for objects that have pre-existing constraints.