Hi,
AFAICT, the proposal does not specify what happens if I try to bypass the constructor of a strong class to create an instance thereof.
Given the class:
"use strong";
class C {
constructor(x) {
this.x = x;
}
}
There are at least two ways to bypass the constructor of a class:
1. Object.create:
"use strong";
const c = Object.create(C.prototype);
or worse, with a property bag:
const c = Object.create(C.prototype, {
y: {value: "erf"}
});
2. Attach C.prototype as prototype of a weak function:
// weak code
function weakFun(x) {
this.y = x;
}
"use strong";
const weakFun = somehowGetWeakFun();
weakFun.prototype = C.prototype;
const c = new weakFun(5);
It seems to me that these things should throw some kind of error (TypeError?). Otherwise, all the invariants about strong objects of class C would be broken.
On a related note, what happens if I invoke the constructor() method with an already constructed object:
"use strong";
const c = new C(5);
C.prototype.constructor.call(c, 7);
Likewise, this should probably throw.
A consequence of these restrictions is that it would impossible, even from weak code, to build a "secondary constructor" for a class like this:
"use strong";
class C {
constructor() {
this.x = 0; // create the field
}
init(x) {
this.x = x; // but truly initialize it here
}
}
and then in weak mode:
function D(x) {
return new C().init(x);
}
D.prototype = C.prototype;
so that I could just call (in weak or strong mode):
const c = new D(5);