Instance constructors

David Bruant david.bruant at
Fri Sep 23 12:20:26 PDT 2011


Currently, WebIDL states (4.5.3):
"There MUST exist an interface prototype object
for every interface
<> defined
The interface prototype object for a particular interface has properties
that correspond to the attributes
<> and
operations <>
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
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

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.
// 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.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list