As you know, as specified in ES6 (or should I say ES 2015?), constructors defined through the class construct cannot be invoked with a preallocated object, i.e., invoked as Foo.call(obj) instead of obj = new Foo.
I have reflected on how to make that just work, but without failing silently on, e.g., builtins. Here are the result of my thoughts:
Basically, constructors (ES functions with a [[Construct]] internal slot) are distinguished between those that can be invoked on a preallocated object (e.g., function foo(baz) { this.bar = baz }) and those that cannot (e.g., the Map builtin). Then, the semantics of super() inside class constructors is carefully reviewed in order to just work for class constructors invoked as Foo.call(obj), and to protest when attempting to do impossible things with, e.g., Map or Array.
―Claude
Hi,
As you know, as specified in ES6 (or should I say ES 2015?), constructors defined through the `class` construct cannot be invoked with a preallocated object, i.e., invoked as `Foo.call(obj)` instead of `obj = new Foo`.
I have reflected on how to make that just work, but without failing silently on, e.g., builtins. Here are the result of my thoughts:
https://gist.github.com/claudepache/ca20472f050443644f29
Basically, constructors (ES functions with a [[Construct]] internal slot) are distinguished between those that can be invoked on a preallocated object (e.g., `function foo(baz) { this.bar = baz }`) and those that cannot (e.g., the `Map` builtin). Then, the semantics of `super()` inside class constructors is carefully reviewed in order to just work for class constructors invoked as `Foo.call(obj)`, and to protest when attempting to do impossible things with, e.g., `Map` or `Array`.
―Claude
As you know, as specified in ES6 (or should I say ES 2015?), constructors defined through the
class
construct cannot be invoked with a preallocated object, i.e., invoked asFoo.call(obj)
instead ofobj = new Foo
.I have reflected on how to make that just work, but without failing silently on, e.g., builtins. Here are the result of my thoughts:
gist.github.com/claudepache/ca20472f050443644f29
Basically, constructors (ES functions with a [[Construct]] internal slot) are distinguished between those that can be invoked on a preallocated object (e.g.,
function foo(baz) { this.bar = baz }
) and those that cannot (e.g., theMap
builtin). Then, the semantics ofsuper()
inside class constructors is carefully reviewed in order to just work for class constructors invoked asFoo.call(obj)
, and to protest when attempting to do impossible things with, e.g.,Map
orArray
.―Claude