Re: [DOM4]: Element.create

It would be nice, in addition to HTMLElement.create, if elements could
be created directly using new and the constructor taking similar
arguments, eg

HTMLElement.create('div', {...}, ...)

is the same as

new HTMLDivElement({...}, ...)

The reason this is nice is that if you mistype the element name, you
get an error message much closer to the typo. It would be nicer still
if the constructors names were short, eg Div instead of
HTMLDivElement.

Needless to say, HTMLElement.create is useful like
document.createElement is useful, for creating unknown elements or for
reflecting on tag names (although the .constructor property could be
used for that instead.)

> Can you give some proper examples where Element.create() makes DOM
> generation simpler? I've only seen vague hand-waving up to now.

To me I think this:

document.body.appendChild(new Div({className: 'warning'}, [new
Text('Danger, Will Robinson!']));

Is preferable to this:

var d = document.createElement('div');
d.className = 'warning';
d.textContent = 'Danger, Will Robinson!';
document.body.appendChild(d);

The former is mercifully only four lines because of textContent; if it
contained markup, it would be even more verbose.

Dominic

Received on Wednesday, 21 September 2011 01:21:57 UTC