2

そのコードを書いたところ、アラート部分でエラーが発生し、this.wordsが検出されていないことを通知しました。コメントがある場所で配列にアクセスできるため、jqueryの部分で「this」の値が変更されると思います。

単語のプロパティをグローバル(何が実行されるのか)にしたくないので、今は立ち往生しています。そこで、「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);
        });
    }
}
4

5 に答える 5

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);
    });
}
于 2012-08-07T16:55:08.057 に答える
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 );
};
于 2012-08-07T16:55:48.000 に答える
2

の値がハンドラーthisで変更される`done()ため、オブジェクトではなくなります。this次のように、のコピーを別の変数に保存することで修正できます。

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);
        });
    }
}
于 2012-08-07T16:54:14.457 に答える
1

次のようにして、ゲーム関数を内部的に保存します。

var _self = game;

そうすれば_self.difficulty_self.words.length、 などを行うと、彼らはそれにアクセスできるようになります。

于 2012-08-07T16:54:36.363 に答える
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も必要ですが、基本的には関数を呼び出して初期化する方法が1つあり、他のアクセスはゲッターを介して行われます。

テストされていないコード

    function game() { var 難易度 = 0; 変数モード = 0; var ワード = []; var that = this; this.loadWords = function() { //単語プール $.ajax({ type:"GET", url:"word.php", data:{mode:that.mode, problem:that.difficulty} }) をリクエストします。 done(function(html) { alert(this.words.length); }); } }

that変数を追加してに設定してthis、その値を囲むのを助けましたが、ajax 呼び出しでは、mode変数を直接呼び出すだけで十分かもしれませんが、 を使用する必要があるかもしれませんthat。また、this関数が問題になる前に、確実ではありませんが、デバッガーを使用して実際に何が起こっているかを確認し、実際に何を使用するかを確認する必要があります。

this最初は問題だった .ajax 呼び出しの内部を見逃していました。

于 2012-08-07T17:04:05.330 に答える