Making class constructors work on preallocated objects

Claude Pache claude.pache at gmail.com
Mon Mar 30 18:52:04 UTC 2015


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




More information about the es-discuss mailing list