Say I have an existing object, a, and a prototype b. I want to make a new object, c, with the values of a, but the prototype of b. Is there a nicer way than the following:
function Copy(object) {
Object.assign(this, object);
}
Copy.prototype = Object.create(b);
var c = new Copy(a);
Edit: Is Object.setPrototypeOf
better or worse than the question solution?
There is no need of having the Copy
constructor. Just use
var c = Object.assign(Object.create(b), a);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With