Object.create()
The Object.create()
method creates a new object, using an existing object
Demo1
Demo2
Demo3
Object.create(Shape.prototype)
is used in the following example for inheritance.
Rectangle.prototype = Object.create(Shape.prototype);
line of code overwrite Rectangle.prototype
field. So, there were Rectangle
constructor in Rectangle.prototype
but after overwritten, constructor field will be removed.
So let's see in debug time. Rectangle.prototype
has a constructor
field which is equal to function Rectangle()
as shown in the following screenshot.
![](https://kenanhancer.com/wp-content/uploads/2020/05/Screenshot-2020-05-24-at-13.47.08.png)
I step over the debugger next line and check the value of Rectangle.prototype
again. Rectangle.prototype
doesn't have a constructor
field this time.
![](https://kenanhancer.com/wp-content/uploads/2020/05/Screenshot-2020-05-24-at-13.50.09.png)
Let me show you Rectangle.prototype.__proto__
. As shown in the screenshot, there is a function Shape()
constructor under __proto__
field.
If you don't set Rectangle.prototype.constructor
to Rectangle, it will take the prototype.constructor of Shape (parent). To avoid that, we set the prototype.constructor to Rectangle (child).
![](https://kenanhancer.com/wp-content/uploads/2020/05/Screenshot-2020-05-24-at-13.53.04.png)
Notice that Rectangle.prototype
has a constructor named function Rectangle(...)
![](https://kenanhancer.com/wp-content/uploads/2020/05/Screenshot-2020-05-24-at-16.07.48.png)
Find the code below
Object.assign()
Object.assign(target, ...sources);