Re: Convenient way to create element and set its attributes at once
Marat Tanalin | tanalin.com <[email protected]>
| Newsgroups | gmane.comp.web.dom.general |
|---|---|
| Message-ID | <[email protected]> |
03.12.2013, 04:04, "Tab Atkins Jr." <[email protected]>: > 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.