new instantiation design alternatives

Allen Wirfs-Brock allen at wirfs-brock.com
Fri Sep 12 10:01:32 PDT 2014


On Sep 12, 2014, at 4:22 AM, Claude Pache wrote:

> 
> 
> Le 12 sept. 2014 à 08:39, Jeff Morrison <lbljeffmo at gmail.com> a écrit :
> 
> That is satisfying for most cases while remaining backward compatible with ES1-5, but it is a priori problematic in the following situations, where one may want manual allocation whereas the rule mandates an automatic one.
> 
> (1) Base classes (i.e., classes without `extends` clause) that want manual allocation. The legacy possibility of returning an alien object from the constructor remains. Alternatively, the following hack is possible, provided that we accept `Function.prototype` as a constructor.
> 
> 	class Base extends Function.prototype { /* ... */  } 
> 
> (IIUC, the prototype chain for both constructor and instances will be the same as without the `extends Function.prototype` clause.) If that situation occurs often, we might want to provide sugar around that.

This is actually how I intend to specify "auto allocation" to work for the `class {}` case.  Function.prototype is a constructor that is specified to unconditionally allocated and return an ordinary object. So, in reality there is no real difference between `class Base {` and `class Base extends Function.prototype {` (ignore that `Function` is a mutable binding).  But in the usage document I found it similar to talk about the no `extends` as a special simple case.

> 
> (2) Constructors defined outside classes that want manual allocation of `this`. For these, I propose the following syntax:
> 
> 	function Derived(a, b) extends Base { /* ... */ }
> 
> as an almost equivalent of:
> 
> 	function Derived(a, b) { /* ... */ }
> 	Derived.__proto__ = Base
> 	Derived.prototype = { __proto__: Base.prototype || Object.prototype, constructor: Derived }
> 
> except that the automatic allocation of `this` does not occurs in the former case. Like constructors defined in classes, the `extends Function.prototype` trick is possible.

I also considered this, but decided that we could avoid talking about what was likely to be a controversial syntactic extension until Es7.

> 
> —Claude
> 	
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140912/bfff7f85/attachment-0001.html>


More information about the es-discuss mailing list