Re: [DOM4]: Element.create

On 21/09/11 11:21 AM, Dominic Cooney wrote:
> 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
>

That's not a proper example, and anyway it would be much simpler to use:

document.body.insertAdjacentHTML("beforeEnd",
     '<div class="warning">Danger, Will Robinson!</div>');

A proper example would be to show how the internals of a popular JS lib 
would be refactored, or a significant example of DOM generation such as 
a table.

Sean

Received on Wednesday, 21 September 2011 01:48:17 UTC