"Subclassing" basic types in DOM - best method?

Tab Atkins Jr. jackalmage at gmail.com
Tue Nov 20 13:52:43 PST 2012


On Tue, Nov 20, 2012 at 1:31 PM, Rick Waldron <waldron.rick at gmail.com> wrote:
> On Tue, Nov 20, 2012 at 2:45 PM, Tab Atkins Jr. <jackalmage at gmail.com>
> wrote:
>> On Mon, Nov 19, 2012 at 9:46 PM, Brendan Eich <brendan at mozilla.com> wrote:
>> > Tab Atkins Jr. wrote:
>> >> If we did this, the only reason to continue subclassing Map is to get
>> >> instanceof checks to work.  Is this acceptable?
>> >
>> > I think it's either irrelevant (no one tests 'aUrlQuery instanceof Map')
>> > or else a potential problem (cross-frame instanceof).
>>
>> People *do* perform those checks, though.  For example, in a method
>> that accepts either an array or other things, a quick "foo instanceof
>> Array" check is a clear, easy way to check what you've got.
>
> Be careful there, it's incredibly rare to see code that does that—which is
> why Array.isArray was created and is generally shimmed with some version of
> ({}).toString.call(arg).slice(8, -1) === "Array"... instanceof is generally
> viewed (whether correctly or not) as "broken".

Of course, that pattern is broken too - it lets you detect actual
Arrays, but not things that subclass Array.  A Map.ismap that works
the same way would completely fail to detect a URLQuery as a Map.


>> (On the other hand, they might instead just feature-test for the
>> existence of items(), and then use it as an iterable, in which case
>> this would be a non-issue.  Depends on the use-case, I suppose.)
>
> Probably not since that will result in a lot of false positives when:
>
> let o = { items: function() {} };
>
> would pass that test.

I'm not sure how else you're supposed to detect iterables.

~TJ


More information about the es-discuss mailing list