Object.create(obj)创建了一个新对象,并指定了obj作为新对象的原型。
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
let duck = Object.create(Animal.prototype);
Animal.prototype.isPrototypeOf(duck) //true
duck instanceof(Animal) //true
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
function Dog() { }
Dog.prototype=Object.create(Animal.prototype)
let beagle = new Dog();
beagle.constructor===Dog // false
beagle.constructor===Animal //true
beagle.constructor===Animal.prototype.constructor //true
Dog.prototype.constructor===Animal //true
添加Dog.prototype.constructor=Dog;之后
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
function Dog() { };
Dog.prototype=Object.create(Animal.prototype);
Dog.prototype.constructor=Dog;
let beagle = new Dog();
beagle.constructor===Dog // true