Home > Enterprise >  What are the differences between the resutls of Dog.prototype = Object.create(Animal.prototype) and
What are the differences between the resutls of Dog.prototype = Object.create(Animal.prototype) and

Time:06-22

What are the differences between the resutls of Dog.prototype = Object.create(Animal.prototype) and Dog.prototype = {...Animal.prototype} ?

CodePudding user response:

On the surface level, one is just copying properties, the other is basically creating a new copy of the Animal:

enter image description here

The destructuring copy also copies all references in the source object, so changes on Dog could then affect Animal, which probably isn't what you want.

CodePudding user response:

If you create a new object (using the shallow copy abilities of the spread operator), then changes to the original object will not be applied to the new one.

CodePudding user response:

There are several differences, including:

  • {...Animal.prototype} will create a separate prototype that lives its own life, unaware of property assignments that might be brought to Animal.prototype

  • {...Animal.prototype} only copies enumerable properties, and as some methods might not be, you'll miss out on them.

  • Related