array like objects [correction]
On Fri, Dec 11, 2009 at 4:33 PM, David-Sarah Hopwood < david-sarah at jacaranda.org> wrote:
David-Sarah Hopwood wrote:
Mark S. Miller wrote:
function isArrayLike(obj) { var len; return !!(obj && typeof obj === 'object' && 'length' in obj && !({}).propertyIsEnumerable.call(obj, 'length') && (len = obj.length) >>> 0 === len); }
Since getting 'length' may have side effects, this is written a bit weird so
that this get only happens after earlier tests pass.
If you want to avoid side effects:
function isArrayLike(obj) { if (!obj || typeof obj !== 'object') return false; var desc = Object.getPropertyDescriptor(obj, 'length');
getOwnPropertyDescriptor, I meant.
Since there is no legacy issue with it, and we don't want to enable code to make stability claims we can't enforce, we wish to keep getOwnPropertyDescriptor meta. Also, for many ES5 objects that wish to be considered array-like, their length will be an accessor property anyway.
I think I buy your argument that we simply shouldn't be testing the value of length in this predicate. If we get rid of that last conjunct, all is well.
2009/12/11 Mark S. Miller <erights at google.com>:
On Fri, Dec 11, 2009 at 4:33 PM, David-Sarah Hopwood <david-sarah at jacaranda.org> wrote:
David-Sarah Hopwood wrote:
Mark S. Miller wrote:
function isArrayLike(obj) { var len; return !!(obj && typeof obj === 'object' && 'length' in obj && !({}).propertyIsEnumerable.call(obj, 'length') && (len = obj.length) >>> 0 === len); }
Since getting 'length' may have side effects, this is written a bit weird so that this get only happens after earlier tests pass.
If you want to avoid side effects:
function isArrayLike(obj) { if (!obj || typeof obj !== 'object') return false; var desc = Object.getPropertyDescriptor(obj, 'length');
getOwnPropertyDescriptor, I meant.
Since there is no legacy issue with it, and we don't want to enable code to make stability claims we can't enforce, we wish to keep getOwnPropertyDescriptor meta. Also, for many ES5 objects that wish to be considered array-like, their length will be an accessor property anyway. I think I buy your argument that we simply shouldn't be testing the value of length in this predicate. If we get rid of that last conjunct, all is well.
It would be nice if the definition of array-like object meant that any for (var i = 0; i < a.length; ++i) ... where the body halts would halt. Obviously sampling length once doesn't tell us that. But not testing length at all doesn't really seem to be getting at the essence of array-ness which is that array iteration is appropriate.
David-Sarah Hopwood wrote:
getOwnPropertyDescriptor, I meant.