On Sat, Dec 12, 2009 at 10:36 AM, Mike Samuel <mikesam...@gmail.com> wrote:
> 2009/12/12 Mike Wilson <mike...@hotmail.com>: > > 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); > >> > } > > Nits: > > Array length is specified as being in [0, 0x8000_0000], but the range > of >>> is [0, 0x1_0000_0000). > > As David-Sarah suggested, the current proposal avoid testing the value of length at all, dodging this issue. > On the String defect, we could repair that with > && ({}).toString.call(obj) !== '[object String]' > Cons: > An extra function call in the likely case > Strings are arguable array-like > Pros: > Strings are inconsistently indexable : (new String('foo'))[0] is > undefined on IE 6 > > Yes, this test would work. Before incorporating it, we should discuss whether String wrapper objects should indeed be considered array-like. My inclination is that they should not. Opinions? > > >> If you want to avoid side effects: > >> > >> function isArrayLike(obj) { > >> if (!obj || typeof obj !== 'object') return false; > >> var desc = Object.getOwnPropertyDescriptor(obj, 'length'); > > What will getOwnPropertyDescriptor do for proxied arrays under the > current proxy proposal? > > It will return undefined, since (by design) it reveals that a trapping proxy does not actually have any own properties. Therefore, the if-test below will not execute its then-part since desc will be falsy. > >> if (desc) { > >> var len = desc.value; > >> return !desc.enumerable && (len === undefined || len >>> > >> 0 === len); > >> } > >> } > > > > An advantage with Mark's code is that it doesn't rely > > on ES5 API. I think it's good to establish a standard > > for array-likeness that can be matched by ES3 code as > > well. > > > > Best regards > > Mike > > > > _______________________________________________ > > es-discuss mailing list > > es-discuss@mozilla.org > > https://mail.mozilla.org/listinfo/es-discuss > > > -- Cheers, --MarkM
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss