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: Marat Tanalin | tanalin.com <mtanalin@yandex.ru>
Date: Tue, 03 Dec 2013 04:37:26 +0400
To: Tab Atkins Jr. <jackalmage@gmail.com>
Cc: Elliott Sprehn <esprehn@gmail.com>, "www-dom@w3.org" <www-dom@w3.org>
Message-Id: <64551386031046@web3g.yandex.ru>
03.12.2013, 04:04, "Tab Atkins Jr." <jackalmage@gmail.com>:
> I find "new Element('h1',...)" less good to read than something like
> "HTML.h1(...)". š(If you have a tagname in a string, you can always do
> HTML[tag](...).)

As for `HTML.h1(...)` specifically, this is a potentially bad idea to have a tag name as a method name.

Tag name may typically be arbitrary and even unsupported by browser while method names are usually predefined and cannot be arbitrary.

Also, there is a risk that some tag name will eventually interfere with some sensible method or property name. For example, if future HTML version will add `length` element, `HTML.length` as an object property and `HTML.length()` as a "constructor" method would have totally different purpose/functionality and therefore be ambiguous/confusing.

Actually, using tag names as method names is probably a _worst_ syntactic idea I've heard of lately.

>> šIt would also be consistent with already existing web-developer-friendly constructors like `new Text` and `new Comment` (implemented in Firefox 24+, Chrome 28+, and Opera 15+).
> Not really. šThere's a single type of Text node and Comment node.
> There are many significant types of Element nodes. šThis isn't a good
> way to slice up the functionality.

If someone would like to use constructors like `new HTMLDivElement`, they are free to do that.

However, if we are talking about a really usable and non-error-prone syntax, this probably shouldn't be the _only_ option. For me as a 10-years+ experienced web-developer, `new Element(arbitraryTagName)` looks like a more suitable option.

Just in case, please note that my original proposal is not about constructor-or-factory-method topic at all. My proposal is about creating element and setting its multiple attributes at once.

Received on Tuesday, 3 December 2013 00:37:56 UTC

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