I'm testing the following code in Chrome Canary 63 and on Chrome 61. The goal of the code is to set a method that calls super onto a class instance (z). In my codebase sometimes property blocks are cloned with Object.assign before being added to the instance and whenever this happens the code fails.
I've reproduced the problem with the code example below. The code works fine if avoid the shallow clone (props = Object.assign({}, props);) but if I add that line I get TypeError: (intermediate value).bar is not a function.
I've tried doing Object.create(this, Object.getOwnPropertyDescriptors(props)) instead of Object.assign but it results in the same error.
Is there any way to properly set super on objects that have been cloned?
let Moo = class {
bar() {
return " [bar on Moo] ";
}
};
let Zoo = class extends Moo {
bar() {
return " [bar on Zoo] " + super.bar();
}
};
function addProps(inst, props) {
// Code works if the line below is commented out but otherwise
// results in TypeError: (intermediate value).bar is not a function
props = Object.assign({}, props); // <-- Offending code
Object.setPrototypeOf(props, inst.__proto__);
Object.assign(inst, props);
}
let props = {
onZooInst: {
bar() {
return " [bar on overridden instance] " + super.bar();
}
}
};
let z = new Zoo();
addProps(z, props.onZooInst);
console.log(z.bar());