[[Call]] vs. [[Construct]] using symbols

Herby Vojčík herby at mailbox.sk
Wed Oct 3 00:29:35 PDT 2012


Hello,

now that we have symbols (and use some of them language-wise, like 
@iterator or proposed @toStringTag), I'd say we can probably dissect 
[[Call]] and [[Construct]] semantics fairly easily. Assuming there are 
global constant symbols @construct and @call, for min-max classes the 
issue is very simple:

class Foo {
	@construct(bar) {
		this.bar = bar;
	}
	@call() {
		// do something else
	}
}

I'd propose the above as the minimal proposal for [[Call]] and 
[[Construct]] separation (even when it only works for classes; and so 
they can be abused to create function with [[Call]] and [[Construct]] 
separated).
Implementation would be to create Foo such that it chooses one or the 
other based on whether [[Call]] or [[Construct]] is invoked.

It could go further, like this:

var f = { @construct() {console.log(1)}, @call() {console.log(2)} };
f(); // logs 2
new f; // logs 1

In case of "native functions" (function, =>) do [[Call]] and 
[[Construct]] as today, in case of objects, check for @construct / @call 
presence.

But this has some quirks to solve, like typeof must be "function" for 
every object having @call, but what about object having only @construct?

Thanks, Herby


More information about the es-discuss mailing list