On 12/28/2012 11:18 AM, Boris Zbarsky wrote:
On 12/28/12 4:12 AM, Neil wrote:
In the interim I believe foo.constructor.name == "Array" was popular,
but I see that doesn't work for (e.g.) new Image().

This should work (modulo bugs in exactly the cases where we've hacked
the instanceof behavior) for WebIDL bindings, actually.  We could also
make it work for XPConnect DOM constructors....

Sadly, it's not much better than using .ownerDocument.defaultView on the
RHS of instanceof in terms of typing and having to remember to do it.

It also falls apart in lots of other cases, due to the lots of people use try to do classes. Since |constructor| is a property of the prototype in JS, that breaks when people do:

function Foo() { /* ... /* }

Foo.prototype = {
  /* ... Foo.prototype.constructor just died ... */
};

(Notice that if you use Foo.prototype.yourPropertyName = /* ... */, Foo.prototype.yourMethodName = /* ... */, and so on for every property and method, the constructor comparison still works.)

I realize the original scope of the discussion *here* is with IDL, but if we start advocating for the constructor comparison approach, people have to be conscious of where it doesn't work (i.e., in these cases) and so they can't universally switch over to a new pattern. It reduces its utility as a pattern.

--
Colby Russell
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to