我有以下
function mild_bird(){
this.name = "catherine";
this.origin = "st petersburg";
this.location = "brighton beach";
}
mild_bird.prototype.get_info = function(){
return "poo" + ", " + "pee";
}
function wild_bird(nickname){
this.nickname = nickname;
//anyway to reference parameters in mild_bird constructor's?
this.name = mild_bird.prototype.name;
this.origin = mild_bird.prototype.origin;
this.location = mild_bird.prototype.location;
}
wild_bird.prototype = new mild_bird();
wild_bird.prototype.constructor = wild_bird;
var the_wild_bird = new wild_bird("sandy");
alert(the_wild_bird.name);
最后一行的警报返回undefined。我希望它能归还“凯瑟琳”。是否可以将mild_bird的构造函数中的属性传递给wild_bird的构造函数?
答案 0 :(得分:1)
您必须在子构造函数中调用父级的构造函数。使用.call(this)
可确保您将对象设置为由子构造函数创建的对象。
function wild_bird(nickname){
mild_bird.call(this);
this.nickname = nickname;
}
答案 1 :(得分:0)