This repository was archived by the owner on Apr 15, 2025. It is now read-only.
This repository was archived by the owner on Apr 15, 2025. It is now read-only.
constructor and record/tuple #115
Closed
Description
Maybe it may seem strange, but any constructor can return a literal object. Sometimes this is interesting in some special classes:
class Point {
constructor(x, y) {
return {x, y};
}
}
const p = new Point(10, 20);
Of course, we cannot return other than a object: https://tc39.es/ecma262/#sec-ecmascript-function-objects-construct-argumentslist-newtarget
As a result, with the current specification of record and tuples we cannot return this kind of elements into a constructor.
class Point {
constructor(x, y) {
return #{x, y};
}
}
const p = new Point(10, 20); // wrong, p is {}
Is it true? Am I right? The idea of immutable object as result of constructor is very atractive for me, but I undestant the special caracteristic of this kind of elements.