2016-05-30 67 views
0

使用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(); 

}); 
+5

錯字工作'this.speak = fucntion' ==>'分號之後function' – charlietfl

+3

也'fucntion'是放錯地方,你的'Jak'和'Cap'變量應該是小寫的,就像它們聲明後的方法調用一樣。 – Bergi

+0

請記住,在javascript'this.speak = function();'後面加上分號,會在您的函數語句甚至在其定義之前終止。因此它始終是'undefined' – element11

回答

3
this.speak = fucntion(); { 
    this.elOutput.innerHTML += "<br>" + this.strId; 
}; 

我想你的意思function()

和JS是區分大小寫的。

也許這是你試圖完成什麼:

$(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 = function() { 
     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.pickUpNumber(); 
}); 

結帳this jsbin

+2

這應該是一個編輯 – Lux

+3

@勒克斯不,這實際上是答案。其他幾個錯別字。 – 4castle

+0

如果這確實是問題,那麼調試器怎麼說'this.speak'是'undefined'?如果出現語法錯誤,代碼甚至無法執行。 –

相關問題