Re: A proposal for Element constructors

Thanks for comments!

Preface: Basically, I do not intend to propose Element constructors
__instead__ of Element.create("button", ...) or new Element("button",
...) or some possible extension of .createElement("button", ...). I
think that both are useful. In this thread, I would like to discuss
whether the Element constructors are worth introducing into the spec
and how the desirable spec should look like.

John-David wrote:
> Something like Element('<div>') is so so soo nice compared with more
> verbose alternatives and you can still add attributes to elements via
> a second argument. I know some prefer smth like Element('div#foo') ->
> <div id="foo"></div> but that get's ugly when trying to expand that
> syntax to an element's children.

In terms of syntax sugar, I agree that Element constructors may lose
Element.create("button", ...), new Element("button", ...) or some
possible extension of .createElement("button", ...). However, Element
constructors are desirable to the consistency:

> (b) Consistency with other constructable DOM objects
> new XMLHttpRequest(), new Image(), new Event(), new CustomEvent(), new
> MessageEvent(), ...

and are required for the sub-typing:

> (c) Enables to subtype DOM objects in the future
> We are planning to make DOM objects subtype-able, like this:
>
>     function MyButton(text) {
>         HTMLButtonElement.call(this);    /* (#) */
>         this.textContent = text;
>     }
>     MyButton.prototype = Object.create(HTMLButtonElement.prototype, {...});
>     var fancyButton = new MyButton("Click Me!!");
>
> In order to make the line (#) work, HTMLButtonElement must have a
> constructor.

Extensiblity of DOM objects by the sub-typing is a fundamental
technique for the Component Model
(http://wiki.whatwg.org/wiki/Component_Model#Extensibility).


Regards


2011年10月27日15:47 John-David Dalton <john.david.dalton@gmail.com>:
> Rick has a point.
> I used Prototype's Element constructor for years and have moved
> towards the jQuery approach in my own API.
> Something like Element('<div>') is so so soo nice compared with more
> verbose alternatives and you can still add attributes to elements via
> a second argument. I know some prefer smth like Element('div#foo') ->
> <div id="foo"></div> but that get's ugly when trying to expand that
> syntax to an element's children. Also the `new` operator should be
> optional as it is with others like `Array`, `RegExp`, and `Function`.
> http://api.jquery.com/jQuery/#jQuery2
>
> Also these mods should be safe for libs like Prototype because they
> pave the Element class with their own custom Element constructor.
>
> - John-David Dalton
>



-- 
Kentaro Hara, Tokyo, Japan (http://haraken.info)

Received on Thursday, 27 October 2011 07:36:13 UTC