2012-08-07 90 views
2

我剛剛寫了那段代碼,並在警報部分得到一個錯誤,告訴我,this.words沒有定義。 我想jQuery的一部分改變了「這個」值,因爲在評論的位置,我可以訪問數組。javascript返回未知屬性

現在我卡住了,因爲我不想讓詞屬性全球(什麼使它運行)。 所以我想問你一個解決問題的方法,同時保持「OOP」風格。

function game() 
{ 
    this.difficulty = 0; 
    this.mode = 0; 
    this.words = new Array(); 

    this.loadWords = function() 
    { 
     //request word pool 
     $.ajax({ 
      type:"GET", 
      url:"word.php", 
      data:{mode:this.mode, difficulty:this.difficulty} 
     }).done(function(html) { 
      alert(this.words.length); 
     }); 
    } 
} 
+0

'this.words = [];' – Radu 2012-08-07 16:56:31

+0

因爲'this'代表全局的'window'。你準備什麼? – SpYk3HH 2012-08-07 16:56:34

回答

2

this值在`done()處理程序更改,因此它不再是你的對象。在內部做喜歡的事,

function game() 
{ 
    this.difficulty = 0; 
    this.mode = 0; 
    this.words = new Array(); 

    this.loadWords = function() 
    { 
     var self = this; 
     //request word pool 
     $.ajax({ 
      type:"GET", 
      url:"word.php", 
      data:{mode:this.mode, difficulty:this.difficulty} 
     }).done(function(html) { 
      alert(self.words.length); 
     }); 
    } 
} 
1

保存的遊戲功能:您可以通過節省掉的this複製到這樣的另一個變量解決它

var _self = game;

_self.difficulty這樣,_self.words.length等等......他們將能夠訪問它。

+2

不推薦使用'self',因爲'self'默認爲'window'的別名。重新分配「自我」可能會產生不良影響。更好地使用'that','_this'等 – jackwanders 2012-08-07 16:56:12

+0

哦對不起,我打算鍵入_self(這是我通常使用的),但你絕對是對的! – 2012-08-07 16:59:09

3

這似乎是一個範圍界定問題。 this不再指.done函數中的遊戲對象。嘗試

this.loadWords = function() 
{ 
    var that = this; 
    //request word pool 
    $.ajax({ 
     type:"GET", 
     url:"word.php", 
     data:{mode:this.mode, difficulty:this.difficulty} 
    }).done(function(html) { 
     alert(that.words.length); 
    }); 
} 
3
function Game() 
{ 
    this.difficulty = 0; 
    this.mode = 0; 
    this.words = new Array(); 
    this.wordsLoaded = $.proxy(this.wordsLoaded, this); 
} 

var method = Game.prototype; 

method.loadWords = function() { 

    $.ajax({ 
     type:"GET", 
     url:"word.php", 
     data:{mode:this.mode, difficulty:this.difficulty}, 
     success: this.wordsLoaded 
    }); 

}; 

method.wordsLoaded = function() { 
    alert(this.words.length); 
}; 
0

你可以看一下

http://javascript.crockford.com/private.html

對更多的想法私有,保護和JavaScript的公衆,但在這裏是一個解決方案:

未經測試的代碼

function game() { 
    var difficulty = 0; 
    var mode = 0; 
    var words = []; 
    this.loadWords = function() 
    { 
     //request word pool 
     $.ajax({ 
      type:"GET", 
      url:"word.php", 
      data:{mode:this.mode, difficulty:this.difficulty} 
     }).done(function(html) { 
      alert(this.words.length); 
     }); 
    } 
} 

你也想爲words獲得一個getter,但基本上有一種方法來初始化它,通過調用該函數並通過getter進行任何其他訪問。

未經測試的代碼

功能遊戲(){ VAR困難= 0; var mode = 0; var words = []; var that = this; this.loadWords =函數() { //請求字池 $就({ 類型: 「GET」, URL: 「word.php」, 數據:{模式:that.mode,難度: that.difficulty} })。done(function(html){ alert(this.words。長度); }); } }

我加了that變量,並將其設置爲this,幫助附上該值,但在AJAX調用它可能足以直接調用mode變量,但它可能需要使用that。此外,功能之前的this可能是一個問題,而不是確定的,我需要使用調試器來查看實際發生的情況並查看實際使用的內容。

我錯過了最初是問題的.ajax調用中的this

+0

不起作用。 「這」保持另一個價值。 – ltsstar 2012-08-07 17:10:48

+0

嘗試刪除'this',但我需要測試並查看會發生什麼,但幾天後我不會有機會這樣做。 – 2012-08-07 20:51:02