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

Rick Waldron <[email protected]>
Newsgroups gmane.comp.web.dom.general
Message-ID <CAHfnhfqLvcrB9=BYGR0spycT8PRd+LFqu-8GCJTJgeDeD2wp1w@mail.gmail.com>
On Mon, Dec 2, 2013 at 7:37 PM, Marat Tanalin | tanalin.com <
[email protected]> wrote:

> 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.
>
>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.