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

± > 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,
François

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