Instance constructors
David Bruant
david.bruant at labri.fr
Fri Sep 23 12:20:26 PDT 2011
Hi,
Currently, WebIDL states (4.5.3):
"There MUST exist an interface prototype object
<http://www.w3.org/TR/2011/WD-WebIDL-20110712/#dfn-interface-prototype-object>
for every interface
<http://www.w3.org/TR/2011/WD-WebIDL-20110712/#dfn-interface> defined
(...)<http://www.w3.org/TR/2011/WD-WebIDL-20110712/#dfn-extended-attribute>.
The interface prototype object for a particular interface has properties
that correspond to the attributes
<http://www.w3.org/TR/2011/WD-WebIDL-20110712/#dfn-attribute> and
operations <http://www.w3.org/TR/2011/WD-WebIDL-20110712/#dfn-operation>
defined on that interface."
This is likely to be motivated by the need to enforce that all
"instances" inherit all constants, attributes and operation from the
interface.
This is fine for operations and constants but forces something i
consider hacky for attributes which is imposing them as getter/setters
on the interface and use the |this| to emulate a value-per-instance
behavior.
One problem this hack is a workaround of is that JavaScript lacks of a
mechanism to enforce that things inheriting from the same prototype
object have some common properties.
When running "Object.create(someObject);", someObject has no way to
initialize the object being created while it may be relevant for it to
add properties.
I'd like to propose the addition of a new internal property of object
which is [[Initializer]] which is a function that is called when an
object is created.
Example:
-----
// assuming o is an object with o [[Initializer]] = function(){this.a = 1}
// Maybe it's a regular object, maybe it's an host. Whatever.
var oo = Object.create(o); // calling o.[[Initializer]] with the object
being created as |this|
Object.getOwnPropertyDescriptor(o, 'a'); // {value: 1} (+true, true, true)
-----
Actually the idea of an internal [[Initializer]] is not very far from
the "constructor" in the classes proposal [1]. I would expect "super" to
work the same which is calling [[Prototype]].[[Initializer]].
I think that [[Initializer]] should be assigned at initialization time
and not be settable afterward, like for [[Prototype]]. Maybe could it be
assigned with an additional Object.create argument/obj init syntax
special field?
Open issue:
- How to pass arguments to [[Initializer]]? (is it absolutely necessary?)
Back to WebIDL, it could define custom [[Initializer]] for each
interface in order to create the relavant properties at the own layer as
data properties rather than the craziness of getter/setter at the
interface level.
David
[1] http://wiki.ecmascript.org/doku.php?id=harmony:classes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110923/bea7907f/attachment.html>
More information about the es-discuss
mailing list