function person(firstname,lastname,age,eyecolor) {
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.changeName=changeName;
function changeName(name) {
this.lastname=name;
}
}
var cont="";
myMother=new person("Steve","Jobs",56,"green");
myMother.changeName("Ballmer");
for (var i in myMother){
cont+=i+":"+myMother[i]+"<br />";
}
document.write(cont);
本人新手,在对JS中的对象方法的调用中对例子产生疑惑,JS中调用方法直接用object.method()就行了,为什么在上面代码中还需要添加这一句代码呢
this.changeName=changeName;
如果把这一句删除了就会显示
myMother.changeName is not a function
为什么需要在person中添加1个changeName 属性才行呢?
等价写法为