JavaScript inheritance with _.extend()

后端 未结 1 1326
感动是毒
感动是毒 2021-01-31 00:28

Whats the difference between

Employee.prototype = Object.create(Person.prototype);

and

_.extend(Employee.prototype, Person.pr         


        
相关标签:
1条回答
  • 2021-01-31 00:38

    With Employee.prototype = Object.create(Person.prototype); you are completely replacing the Employee.prototype.

    But with _.extend(Employee.prototype, Person.prototype); you are adding the Person.prototype on top of the Employee.prototype.

    For example,

    var a = {var1:1, var2:2};
    var b = {var2:4, var3:3};
    console.log(_.extend(a, b)); // {var1:1, var2:4, var3:3}
    

    As you see, a it's not completely replaced by b, it's just extended by the properties defined in b.

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