使用Firebug它不斷指出this.speak沒有定義,我不明白爲什麼is.I我試圖將它輸出到屏幕爲什麼會發生這種不停地說這不是definied
$(document).ready (function() {
function person(rank, number, id) {
//properties
this.intRank = rank;
this.intNumber = number;
this.strId = id;
this.elOutput = document.getElementById(id);
//methods
this.speak = fucntion(); {
this.elOutput.innerHTML += "<br>" + this.strId;
};
//adds one to int number and speaks message
this.pickUpNumber = function() {
this.intNumber++;
this.strId = "i have" + this.intNumber.toString() + "rocks";
this.speak();
};
};
//object
var Jak = new person(5, "hey ,jack", " Captain");
var Cap = new person(3, "yea cap?", "jacko");
jak.speak(); cap.speak(); jak.pickUpRock();
});
錯字工作'this.speak = fucntion' ==>'分號之後function' – charlietfl
也'fucntion'是放錯地方,你的'Jak'和'Cap'變量應該是小寫的,就像它們聲明後的方法調用一樣。 – Bergi
請記住,在javascript'this.speak = function();'後面加上分號,會在您的函數語句甚至在其定義之前終止。因此它始終是'undefined' – element11