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: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Mon, 2 Dec 2013 11:08:36 -0800
Message-ID: <CAAWBYDCCnq9hLTwz_4P+1LNceUgEjnJ=fmJagx0TbLem7UNvXQ@mail.gmail.com>
To: Domenic Denicola <domenic@domenicdenicola.com>
Cc: Elliott Sprehn <esprehn@gmail.com>, "Marat Tanalin | tanalin.com" <mtanalin@yandex.ru>, "www-dom@w3.org" <www-dom@w3.org>
On Mon, Dec 2, 2013 at 11:00 AM, Domenic Denicola
<domenic@domenicdenicola.com> wrote:
> From: Tab Atkins Jr. <jackalmage@gmail.com>
>
>> Hm, I don't understand.  instanceof just walks the prototype chains, and the functions denoted by "HTMLHeadingElement" and "HTML.h1" can have the same prototype.  I suspect I'm missing something in your statement.
>
> Well, it depends on how HTML.h1 was implemented I guess... What were you thinking?
>
> Another way to put it is that you generally want the following invariants:
>
> new X() instanceof X
> (new X()).constructor === X
> Object.getPrototypeOf(new X()) === X.prototype
> X.prototype.constructor === X
>
> I can't really see a solution that makes HTML.h1 a constructor that follows these identities without creating a new class hierarchy separate from HTMLHeadingElement.

Right, you can't satisfy #2 and #4 if you have multiple constructors.
You can satisfy #1 and #3 if you implement things in the obvious way:

HTML.h1 = function(...args) { return new HTMLHeadingElement("h1", ...args); };
HTML.h1.prototype = HTMLHeadingElement.prototype;

~TJ
Received on Monday, 2 December 2013 19:09:34 UTC

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