2016-05-30 1 views
0

それは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

セミコロンjsbin作業

$(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あなたの 'Jak'と' Cap'変数は宣言に続くメソッド呼び出しのように小文字でなければなりません。 – Bergi

+0

javascript 'this.speak = function();の直後にセミコロンをつけて、偶数定義の前に関数文を終了します。したがって、常に未定義です – element11

答えて

3
this.speak = fucntion(); { 
    this.elOutput.innerHTML += "<br>" + this.strId; 
}; 
への出力、それをしようとしていますなぜ私は理解していない放火犯を使用して

は、私はあなたがfunction()

を意味すると思うJSは、大文字と小文字が区別されます。

は、おそらくこれはあなたが達成しようとしたものです:fucntion`は `後

+2

これは編集 – Lux

+3

@Luxでなければなりません。これは文字通り答えです。他のいくつかのタイプミスの中で。 – 4castle

+0

これが本当に問題であれば、デバッガは 'this.speak'が' undefined'と言うことができますか?構文エラーがある場合でもコードを実行することはできません。 –

関連する問題