Re: new

Mon, 17 Oct 2011 01:17:50 -0700

I think we should just forget about new keyword, prototype property all 
togather and move towards something more simple: 

var proto = {
  method: function() { },
  new: function() {
    var self = Object.create(this);
    this.initialize.apply(self, arguments);
    return self;
  },
  initialize: function(someValue) {
    this.things = someValue
  }
}

var o = proto.new(someValue);

Of course with a help of library that would not require as much boilerplate: 
https://github.com/Gozala/selfish  


Regards
--
Irakli Gozalishvili
Web: http://www.jeditoolkit.com/
Address: 29 Rue Saint-Georges, 75009 Paris, France (http://goo.gl/maps/3CHu)


On Tuesday, 2011-10-11 at 20:53 , Jake Verbaten wrote:

> 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
> [email protected] (mailto:[email protected])
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to