Prototype copy vs Object.create() vs new

前端 未结 2 1220
情话喂你
情话喂你 2020-12-05 22:06

I was using inheritance and I noticed that there are three ways to get the same result. What\'s the difference?

function Animal(){
}

Animal.prototype.doThat         


        
2条回答
  •  眼角桃花
    2020-12-05 22:25

    You could either use Solution 1 or Solution 2, but you should not use Solution 3.

    The reason is when you do Bird.prototype = Animal.prototype;, every change on Bird.prototype will also affect Animal.prototype, because they the same object.

    For example, you attach a method on Bird.prototype, then the method will also works on Animal, which will not be what you want.

提交回复
热议问题