當我運行這個腳本時,我在警告框中獲得說話功能代碼。應該是「你好!」不是function(){alert(「Hello!」)}; 。我使用警報,因爲我似乎更喜歡使用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);
謝謝,我也拿出了警報,並用myMother.speak()替換了它。因爲這是雙重警報。再次感謝你 – 2012-07-31 00:41:35
是的,我意識到,所以我改變了答案,以避免雙重警報。 – 2012-07-31 00:42:27
您可能會對此進行擴展,以解釋爲什麼,對於未來發現它的新JS程序員。 – 2012-07-31 00:50:15