所以这是我的JavaScript:
我有一个Cat对象,它扩展了Mammal的原型.哺乳动物有run()方法.但是当我创建新的Cat对象并调用run()时,它告诉我它未定义:
function Mammal(config) {
this.config = config;
}
Mammal.prototype.run = function () {
console.log(this.config["name"] + "is running!");
}
function Cat(config) {
// call parent constructor
Mammal.call(this, config);
}
Cat.prototype = Object.create(Mammal);
var felix = new Cat({
"name": "Felix"
});
felix.run();
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?
它应该是Cat.prototype = Object.create(Mammal.prototype),这是方法的地方,而不是Mammal直接.
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |