(How) does proto operator distiguish between function and non-function in LHS?

Allen Wirfs-Brock allen at wirfs-brock.com
Sun Jan 29 12:17:05 PST 2012


On Jan 28, 2012, at 12:28 AM, Herby Vojčík wrote:

> The proposal for <| cites these usage examples:
> 
> - Setting the prototype of a function to something other than Function.prototype
> 
>    let f = EnhancedFunctionPrototype <| function () {}
> 
> - Parrallel constructor/instance prototype chains
> 
>    let superclass = function () {};
>    //define a constructor method
>    superclass.new = function () {return (new this()).initialize()};
>    //define a instance method
>    superclass.prototype.initialize = function () {return this};
>    let subclass = superclass <| function () {};
>    ...
> 
> Question: When RHS is function expression, does it distinguish if LHS is function to select one of the (different) behaviours from above?

from the proposal: "If the LHS operand has a property named prototype and the RHS operand is a function expression then the [[Prototype]] of the function object is set to the LHS object and the prototype property of the new function is set to a new object whose [[Prototype]] is the value of the LHS’s prototype property. Here’s a picture of what happens in this case: function "subclassing" UML diagram"

In other words, it uses the value of <LHS>.prototype to set the [[Prototype]] of the new prototype object.  If no such property exists, [[Prototype]] of the prototype is set to its default value. 

In all likelihood you would want EnhancedFunctionPrototype to be defined similarly to this  example from (https://github.com/allenwb/ESnext-experiments/blob/master/ST80collections-exp0.js ) of doing something different:
	//define a non constructible superclass that provides some Smalltalk-like conventions
	const AbstractClass = Function.prototype <| {
	  subclassResponsibility() {throw new Error(this.name+" did not implemented an abstract method")},
	  shouldNotImplement() {throw new Error(this.name+" should not implemented by "+this.name)},
	  name: "AbstractClass",
	  prototype: Object.prototype <|{
		get class() {return this.constructor},
		error(message) {throw new Error(message)},
		subclassResponsibility() {return this.class.subclassResponsibility()},
		shouldNotImplement() {return this.class.shouldNotImplement()},
		errorSubscriptBounds(index) {this.error("subscript is out of bounds: "+index)}
	  }
	};


Allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120129/a4d4c352/attachment.html>


More information about the es-discuss mailing list