Tom Van Cutsem (2013-07-31T10:40:39.000Z)
domenic at domenicdenicola.com (2013-08-04T22:36:42.523Z)
2013/7/31 Allen Wirfs-Brock <allen at wirfs-brock.com> > We could define a new predicate Array.isArrayLike with a proxy friendly > extensions mechanism (@@isArrayLike). It wouldn't be the same test as > Array.isArray and the result would be more a statement of intent than an > absolute guarantee. > > Would anybody use it? Would it's existence be enough to eliminate the > Array.isArray/proxy concern? > Adding yet another function doesn't solve the problem: there is extant ES5 code out there that uses Array.isArray. It will fail on proxies-for-arrays, even when those proxies-for-arrays would be perfectly substitutable for actual arrays (e.g. membraned arrays). Here's a contrived example where a function behaves differently depending on whether you pass it an array or not: ```js // call makePoint( [ x, y] ) to construct a new Point(x,y) // call makePoint(x) to construct a new Point(x,x) function makePoint(arg) { if (Array.isArray(arg)) { return new Point(arg[0], arg[1]); } else { return new Point(arg, arg); } } ``` While looking for Array.isArray on GitHub I found an example of an in-the-wild function that does this kind of argument-inspection (even twice in the same function): https://github.com/cnicq/DB/blob/1f3a88501f819378ec7919b5f87d20bbd98da5a7/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/repl_set/strategies/statistics_strategy.js#L19 In particular, this line of code looks like a common idiom when you want to abstract over dealing with either 1 or many things: ```js var tagObjects = Array.isArray(tags) ? tags : [tags]; // then do a for-loop over the array ``` Given that ES6 will allow users to create subclasses of Array, where subclass instances will be branded as arrays, Array.isArray's contract will already become more diluted. In that light, I don't see the problem with Array.isArray(proxyForArray) returning `true`. On the flip side: DOM nodelists, while behaviorally similar to arrays, also don't pass the Array.isArray test. Does this anger developers? Or is this common wisdom and people have learned to live with the differences between nodelists and arrays?