Instance constructors

Allen Wirfs-Brock allen at
Fri Sep 23 15:35:38 PDT 2011

let instance = Object.create(o);
instance.constructor(/* constructor args */)

This assumes that the new instance will inherit "constructor" from its prototype chain (possibly all the way to Object.prototype.constructor (.i.e. Object).
If you are careful to always code your constructors to return this, you can shorten it to:

let instance  = Object.create(o).constructor(/* constructor args */);

As I think I have talked about in the past.  For I think we should extend the semantics of new to be approximately:

function ESNewOperator( rhs, args) {
   if (rhs has [[Constructor]] ) return rhs.[[Constructor]].apply(undefined,args);
   let instance = Object.create(rhs);
   return instance;
( a few additional error conditions need to be added)

This allows the new operator  to make sense (and work in an internally interoperable manner) for both prototypical and classical inheritance

For example:

let proto = {
   depth: 0;
   constructor: function() {this.depth +=  1};  /* get parent's depth, increment it, and store as own property of instance */

let lev1 = new proto;
let lev2 = new lev1;
let lev3 = new lev2;

I'm being terse in my explanation, but this would essentially giving ES the ability to really do self-style prototypical instantiation without getting in the way to existing constructor-based instantiation.  Newing an arbitrary object creates a new instance whose [[Prototype]] is the newed object and then calls the inherited constructor.   The constructor method servers as the initialize method.  Such a constructor  can be called either explicitly as has traditionally been done in JS or it call be called implicitly by newing an that inherits its as its constructor property. 

Regarding, WebIDL.  It seems to me, that it just needs an extended attribute to specifies which attributes are instance attributes rather than prototype attributes. 


More information about the es-discuss mailing list