W3C home > Mailing lists > Public > public-webapps@w3.org > October to December 2011

Re: A proposal for Element constructors

From: Kentaro Hara <haraken@chromium.org>
Date: Thu, 27 Oct 2011 16:35:07 +0900
Message-ID: <CABg10jxqOVvUaeovfajoc0oH_yJO03y8W0wMTCfdzHpSDZdJyA@mail.gmail.com>
To: John-David Dalton <john.david.dalton@gmail.com>, public-webapps@w3.org
Cc: Rick Waldron <waldron.rick@gmail.com>, Dominic Cooney <dominicc@chromium.org>
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 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:48 GMT