[英]'Prototype' throws TypeError: info is undefined
I am trying to inherit a function by prototype, it seems all are correct (according to me), but I am getting error... any one help me to understand this issue? 我正在尝试通过原型继承一个函数,看来一切都正确(根据我的观点),但是我遇到了错误……有人可以帮助我理解此问题吗?
function: 功能:
(function($){
var Man = function(info){
this.name = info.name;
this.age = info.age;
this.work = info.work;
}
Man.prototype.tell = function(){
return 'I am Mr.' + this.name;
}
var Dog = function(info){
Man.call(this,info);
}
Dog.prototype = new Man();
var dog1 = new Dog({name:'Dobber',age:3,work:'roming'});
console.log(dog1.tell());
})($);
error I am getting: 我得到的错误:
TypeError: info is undefined
When you call new Man()
setting up the Dog
prototype, you're not passing an argument. 调用new Man()
设置Dog
原型时,没有传递参数。 The function expects one (that's what info
is supposed to be). 该函数需要一个(这就是应该的info
)。
I'm not sure how to correct it because the setup in that code doesn't make a lot of sense. 我不确定如何纠正它,因为该代码中的设置没有多大意义。
edit — maybe just make the Man
constructor check the parameter: 编辑 —也许只是让Man
构造函数检查参数:
function Man(info) {
if (info !== undefined) {
this.name = info.name;
this.age = info.age;
this.work = info.work;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.