JavaScript style Object.create with existing object

Say I have an existing object, a and prototype b. I want to create a new object c, with values ​​a, but prototype b. Is there a better 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 resolving the issue?

+5
source share
1 answer

There is no need to have a Copy constructor. Just use

 var c = Object.assign(Object.create(b), a); 
+3
source

Source: https://habr.com/ru/post/1209492/


All Articles