使用`Object.create‘进行继承的好处
Object.create
var Animal = function(name) { this.name = name; }Animal.prototype.print = function() { console.log(this.name); }var Dog = function() { return Animal.call(this, 'Dog'); }Dog.prototype = new Animal();Dog.prototype.bark = function() { console.log('bark'); }
var dog1 = new Dog();dog1.print(); // prints 'Dog'dog1.bark(); // prints 'bark'dog1.name; //prints 'Dog'
Dog.prototype = new Animal();
Dog.prototype = Object.create(Animal.prototype);
Object.create
Dog.prototype = Animal.prototype;
慕哥9229398
相关分类