new

Tue, 11 Oct 2011 11:54:06 -0700

is there any kind of proposal for syntax that is like :

var proto = {
  method: function() { },
  constructor: function(someValue) {
    this.things = someValue
  }
};

var o = new proto(someValue);
Object.getPrototype(o) === proto; // true

basically defining how `new` operates on an object (that is to represent the
prototype). It is basically equivelant to

var Constructor = function () { ... }
constructor.prototype = {
  method: ...
  constructor: Constructor
}

var o = new Constructor(someValue);

To me personally the whole duplication the Constructor on
Constructor.prototype.constructor and having .prototype on a function is
less elegant.

Initial concerns with such syntax :

 - new { ... }(parameters) may be difficult to parse
 - new protoObj.call(...) is not going to work (protoObj does not have
Function.prototype in it's prototype chain), should it work?
 - new protoObj(); should probably throw an error if protoObj.constructor is
not defined.
 - should we invoke a constructor property of a protoObj that is not it's
own property
 - the new operator is confusing enough as it is

Advantages:

 - this syntax would map more closely with the proposed declarative class
syntax. So class could just be a sugar for generating objects you can invoke
with new.
 - For any well formed existing code (where .prototype.constructor is set)
new existingObject() should generate a new object with existingobject as
it's prototype and having called the nearest constructor property
 - constructor property is optional so new someObject is just sugar for
Object.create(someObject)
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to