如何在js的prototype中声明多个方法?(在线等)

时间:2022-04-11 18:47:25
<script language="javascript">

function Person(){
this.name = "zhangSan";
this.sex = "男";
this.age = 88;
}

Person.prototype={
getSex():function(){        //这一行提示缺少:
return this.sex;
},
getAge():function(){
return this.age;
}
};

var obj = new Person();
document.write(obj.getSex());
document.write(obj.getAge());

</script>

3 个解决方案

#1


Person.prototype={
getSex ():function(){ //这一行提示缺少:
return this.sex;
},
getAge ():function(){
return this.age;
}
};
括号去掉

#2


Person.prototype={
getSex:function(){ 
return this.sex;
},
getAge:function(){
return this.age;
}
};

#3


还真是,谢谢了,呵呵

#1


Person.prototype={
getSex ():function(){ //这一行提示缺少:
return this.sex;
},
getAge ():function(){
return this.age;
}
};
括号去掉

#2


Person.prototype={
getSex:function(){ 
return this.sex;
},
getAge:function(){
return this.age;
}
};

#3


还真是,谢谢了,呵呵