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: Rick Waldron <waldron.rick@gmail.com>
Date: Mon, 2 Dec 2013 21:05:01 -0500
Message-ID: <CAHfnhfqLvcrB9=BYGR0spycT8PRd+LFqu-8GCJTJgeDeD2wp1w@mail.gmail.com>
To: "Marat Tanalin | tanalin.com" <mtanalin@yandex.ru>
Cc: "Tab Atkins Jr." <jackalmage@gmail.com>, Elliott Sprehn <esprehn@gmail.com>, "www-dom@w3.org" <www-dom@w3.org>
On Mon, Dec 2, 2013 at 7:37 PM, Marat Tanalin | tanalin.com <
mtanalin@yandex.ru> wrote:

> 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.
>
>
Yes, and it's not just you--the "single function/constructor/method that
accepts a string and produces a node" is a common pattern implemented by
all major libraries:

Y.Node.create("<div>");
jQuery("<div>");
dojo.create("div");
new Element("div");
new Ext.dom.Element("div");



Rick
Received on Tuesday, 3 December 2013 02:05:49 UTC

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