You've confused Constructor.prototype
with instance
's internal [[Prototype]]
, what Object.setPrototypeOf()
sets.
You should use the former in this case:
let shape = {
type: 10,
getTyp() {
return "triangle";
}
};
function Triangle() {}
Triangle.prototype=shape;
let t = new Triangle();
console.dir(t.type); //10
The above works, but, to ensure class constructor
point to the correct function, don't overwrite the Constructor.prototype
. Instead, assign properties to it:
let shape = {
type: 10,
getTyp() {
return "triangle";
}
};
function Triangle() {}
Object.assign(Triangle.prototype,shape);
let t = new Triangle();
console.dir(t.type); //10