W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2013

RE: Convenient way to create element and set its attributes at once

From: François REMY <francois.remy.dev@outlook.com>
Date: Mon, 2 Dec 2013 23:43:00 +0100
Message-ID: <DUB402-EAS371A7537B8487658BF4B015A5EA0@phx.gbl>
To: "'Boris Zbarsky'" <bzbarsky@MIT.EDU>, <www-dom@w3.org>
CC: "'Domenic Denicola'" <domenic@domenicdenicola.com>, "'Tab Atkins Jr.'" <jackalmage@gmail.com>
± > I guess it would not be a constructor in that case though, i.e. you'd do `var
± > myH1 = HTML.h1()` instead of `var myH1 = new HTML.h1()`, since otherwise
± > you would break the `new X instanceof X` invariant.
± This invariant is already broken when x is Image or Option or Audio, fwiw.  Or
± any other case that uses WebIDL's NamedConstructor....

This isn't true, at least in IE and FireFox. 

1) new Image() instanceof Image == true
2) new Image() instanceof HTMLImageElement == true
3) document.createElement('img') instanceof Image == true
4) document.createElement('img') instanceof HTMLImageElement == true
5) Image !== HTMLImageElement

However, there's a bug in Chrome where new Image() and document.createElement('img') returns two objects with a different prototype chain (ie: new Image() instanceof Image, new Image() instanceof HTMLImageElement, but document.createElement('img') not-instanceof Image).

This bug means that setting Image.prototype.doSomething has no effect on <img> elements not created via new Image() in Chrome, which is not the case in IE/FireFox.

That being said, I agree that (new X() instanceof X) is not a requirement in JavaScript:

| function xhr() { return new XMLHttpRequest(); }
| new xhr() istanceof xhr == false

± instanceof walks up the proto chain but then looks for the 
± .constructor of the things on the proto chain matching the RHS.

As far as I know, this also isn't accurate.

| var T = function T() {}
| T.prototype = HTMLImageElement.prototype
| document.createElement('img') instanceof T == true

And this is true in all browsers. Also:

| function xhr() { return new XMLHttpRequest(); }
| xhr.prototype = XMLHttpRequest.prototype;
| new xhr() istanceof xhr == true

Best regards,
Received on Monday, 2 December 2013 22:43:34 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 20 October 2015 10:46:21 UTC