假设我有一个名为Cat
的对象在一个节点模块中。我想要替换Cat
函数本身而不是它的原型。换句话说,我想把一个对象的原型添加到另一个对象中。
function Cat(name, breed){
this.name = name
this.breed = breed
}
Cat.prototype.sayName = function(){
console.log(this.name)
}
Cat.prototype.sayBreed = function(){
console.log(this.breed)
}
module.export = Cat
然后我有这个文件:
var _ = require("underscore")
var inherit = require('util').inherits;
var Cat = require("./cat")
function Dog(name, breed){
this.name = name
this.breed = breed
}
// Tries:
// _.extend(Dog, Cat) // logs: {}
// inherit(Dog, Cat) // error: The super constructor to `inherits` must have a prototype.
// Dog.prototype.sayName = Cat.prototype.sayName // Cannot read property 'sayName' of undefined
// Dog.prototype.sayBreed = Cat.prototype.sayBreed
var dog = new Dog("wilmer", "huskey")
console.log(dog.__proto__)
我如何将Cat
的所有原型导入/扩展/继承到Dog
中?