New ES6 constructor features and semantics: Alternative 1 auto allocation in derived classes

September 17, 2014 ยท View on GitHub

Constructor Semantics Summary

  1. Constructors may be invoked either as a "function call" or by a new operator
  2. function call: Foo(arg)
  3. new operator: new Foo(arg)
  4. Within a constructor, the new^ token can be used as a PrimaryExpression to determine how the construtor was invoked
  5. If the value of new^ is undefined then the current function invocation was as a function call.
  6. If the value of new^ is not undefined then the current function invocation was via the new opertor and the value of new^ is the constructor functon that new was originally applied to .
  7. When a constructor is invoked using the new operator it has two responsibility
  8. Allocate (or otherwise provide) a this object
  9. Initialize the this object prior to returning it to the caller
  10. The allocation step may be performed automatially prior to evaluating the constructor body or it may be manually performed by code within the consturctor body.
  11. Automatic allocation is the default. 1. The value of this is initialized to the result of automatic allocation prior to evaluating the body of the constructor.
  12. If a constructor body contains an assignment of the form this = then automatic allocation is not performed and the constructor is expected to perform manual allocation. 1. The value of this is uninitialized (in its TDZ) upon entry to the body of a manually allocating constructor. 2. Referencing (either explicitly or implicitly) an unitialized this will throw a ReferenceError. 2. The first evaluation of an assignment to this initializes it. 3. this may be dyanmically asigned to only once within an evaluation of a manaully allocating constructor body. 4. When a constructor is invoked via a function call (ie, via [[Call]]), this is preinitialized using the normal function call rules. Any dynamic assignment to this during such an invocation will throw a ReferenceError.
  13. Automatic allocation actions:
  14. For a constructor defined using a FunctionDeclaration, FunctionExpression, or by a class definition that does not have an extends clause, the automatic allocation action is to allocated an ordinary object.
  15. For a constructor defined by a class definition that includes a extends clause, the automatic allocation action is to perform the equivalent of: new super(...arguments)
  16. Executing new super in a constructor (that was itself invoked using the new operator) invokes its superclass constructor using the same new^ value as the invoking constructor.