当我运行这个脚本时,我在警告框中得到了说话功能代码。应该是“你好!” 不是函数(){警报(“你好!”)};. 我使用 alert 是因为我似乎更喜欢 console.log 来学习。该脚本在没有说话功能的情况下工作正常。
function person(firstname,lastname,age,eyecolor) {
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.newlastname=newlastname;
this.speak=function(){alert("Hello!")};
}
var myFather=new person("John", "Doe", 45, "blue");
var myMother=new person("Sally","Rally", 48,"green");
function newlastname(new_lastname) {
this.lastname=new_lastname;
}
myMother.newlastname("Doe");
alert(myMother.lastname);
alert(myMother.speak);