I would like to instantiate classes without calling new
by using Object.create
(which is made for it), but how can I get all properties defined aswell?
class Vec2 {
x = 0;
y = 0;
}
a = new Vec2;
b = Object.create(Vec2.prototype);
console.log(a.x, a.y);
console.log(b.x, b.y);
a.x
and a.y
exist, but b.x
and b.y
do not.
Appendum for Bergi comments:
[1]
function Vec1() {
this.x = 0;
}
b = Object.create(Vec1.prototype);
Vec1.apply(b);
[2]
class Vec3 {
x = console.log("x = ...");
constructor() {
console.log("constructor");
}
y = console.log("y = ...");
}
vec3 = new Vec3;