Posts

Showing posts with the label esprop

ECMAScript proposal: function-callable classes

This blog post describes the proposed ECMAScript feature “ call constructor ” ( stage 1 ). Classes can’t be function-called in ES6 In ECMAScript 6, classes can’t be function-called: class Point { constructor(x, y) { this.x = x; this.y = y; } } let p1 = new Point(1, 2); // OK let p2 = Point(3, 4); // TypeError For ES6, there was no categorical objection to classes being function-called. The error was only thrown so that handling function calls via classes could be added later. Function-callable constructors in ES6 If you want to implement a constructor Point that can be both function-called and constructor-called, you have no choice but to use a constructor function in ES6: function Point(x, y) { if (!new.target) { // Point was function-called return new Point(x, y); } this.x = x; this.y = y; } let p1 = new Point(1, 2); // OK let p2 = Point(3, 4); // OK Functi...