What are the differences between Rectangle.prototype = Object.create(Shape.prototype) and Rectangle.prototype = Shape.prototype?

前端 未结 1 1682
死守一世寂寞
死守一世寂寞 2021-01-25 12:15

I was reading the following codes, and start to wonder What are the differences between Rectangle.prototype = Object.create(Shape.prototype) and Rectangle.pro

相关标签:
1条回答
  • 2021-01-25 12:58

    Rectangle.prototype = Shape.prototype; is pointing Rectangle's prototype to the same object as Shape's prototype, meaning both prototypes are now the same object. So if you edit Rectangle.prototype.method it will also appear at Shape.prototype.method.

    Rectangle.prototype = Object.create(Shape.prototype); is creating a new object that inherits Shape's prototype, and assigning to Rectangle's, meaning if you edit Rectangle's prototype from this point, it won't affect Shape's. But if you edit Shape's prototype, you will get the same properties in Rectangle by inheritance.

    Have a play - https://jsfiddle.net/j8o10zfn/

    0 讨论(0)
提交回复
热议问题