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

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.

Thanks.

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