2012-08-07 85 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