Making class constructors work on preallocated objects

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


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 `` 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) { = 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 ``, and to protest when attempting to do impossible things with, e.g., `Map` or `Array`.


More information about the es-discuss mailing list