我正在尝试一个代码,它一直返回“在singerprofile函数下未定义的级别,目前我不知道问题出在哪里。有人知道吗?
function Singer(name,specialty,power,hitpoints,level,gender,rarity){
this.name=name;
this.specialty=specialty;
this.power=power;
this.hitpoints=hitpoints;
this.level=level;
this.gender=gender;
this.rarity=rarity
}
var momobae=new Singer("momobae","k-pop",49,28,7,"female","Rare")
var minibae=new Singer("minibae","k-pop",30,20,5,"male","common")
var array=[momobae,minibae]
this.singerProfile=function(){
console.log(name+" Level"+ level+" gender"+ gender+" power"+power+"!"+" Hitpoints:")
}
for(var i=0;i<array.length;i++){this.singerProfile(array[i])
};
答案 0 :(得分:1)
您必须修复singerProfile
并将当前元素作为参数传递
function Singer(name,specialty,power,hitpoints,level,gender,rarity){
this.name=name;
this.specialty=specialty;
this.power=power;
this.hitpoints=hitpoints;
this.level=level;
this.gender=gender;
this.rarity=rarity
}
var momobae=new Singer("momobae","k-pop",49,28,7,"female","Rare")
var minibae=new Singer("minibae","k-pop",30,20,5,"male","common")
var array=[momobae,minibae]
this.singerProfile=function(elem){
console.log(elem.name+" Level"+ elem.level+" gender"+ elem.gender+" power"+elem.power+"!"+" Hitpoints:")
}
for(var i=0;i<array.length;i++){
this.singerProfile(array[i])
};