0

私はjavascriptに少し慣れていないので、次の実装に問題があります(2日間)。

Object()、'engine'があり、投稿の内容に応じて異なる応答をするようにしたいとします。つまり、ユーザーによるアクションがあり、エンジンがアクションを解釈し、サーバーにリクエストを送信し、サーバーが応答すると、「エンジン」がそれに応じて動作します。

望ましい動作の最小限の例として、仮想的にマウスクリックの位置を受信して​​いる「エンジン」(receiveClickEvent)を提示し、「投稿」(requestByAsyncPost)を介して送信し、その内容(respondToRequest)に従って応答します。

投稿を行うための小さな関数

function requestByAsyncPost(content, funct)
{
/* some initialization missing here to not confuse the reader */
xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            funct(xmlhttp.response);
        }
    }
xmlhttp.open("POST","foobar.php", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(content);
}

var engine = new Object()
engine.respondToRequest = function(content) {/*implementation*/ }

マウスクリックが発生したときにアクティブにしたい小さなコード(トリガー方法は関係ありません)

engine.receiveClickEvent = function(position)
{
    var funct = function(content)
    {
         this.respondToRequest(content); //<<<---- I want to refer engine's 'this'
    }
    requestByAsyncPost('request=click&position=['+position[0]+','+position[1]+')', funct);
}

「this」は関数関数内にあるため、これは私が望むようには機能していません。この種の動作をどのように実装できますか?

4

5 に答える 5

2

thisこのように、または他の方法でアウターを覚えておく必要があります。好き:

engine.receiveClickEvent = function(position)
{
    var self = this;
    var funct = function(content)
    {
         self.respondToRequest(content); //<<<---- 'self' refers engine's 'this'
    }; // <<<--- btw, missed semicolon
    requestByAsyncPost('request=click&position=['+position[0]+','+position[1]+')', funct);
}; // <<<--- btw, here, too :-)
于 2012-11-04T15:03:41.147 に答える
1

匿名の AJAX コールバック関数を多用すると、同じ問題に常に出くわします。

これは、このタイプの問題を「回避」する方法です。

var engine = function (args) {
    // store a reference to "this" inside a variable for reuse in another scope
    var me = this;
    var fetchSomething = function (args) {
        var req = $.ajax({
            method: 'POST',
            url: url,
            success: function(data) {
                // use "me" instead of "this", because "this" is scope dependant
                me.doSomething(data);
            }
        });
    }
    var doSomething = function (data) {
        // some logic here.
    }  
} 
foo = new engine({arg: 'foo', arg2: 'bar'});
foo.fetchSomething(url);
于 2012-11-04T15:04:58.527 に答える
0

現在のthisをいつでも変数に保存して、好きな場所で使用できます。

var that = this;
于 2012-11-04T15:02:44.730 に答える
0

これを試して:

engine.receiveClickEvent = function(position)
{

    var me = this;
    var funct = function(content)
    {
         me.respondToRequest(content); //<<<---- I want to refer engine's 'this'
    }
    requestByAsyncPost('request=click&position=['+position[0]+','+position[1]+')', funct);
}
于 2012-11-04T15:03:18.633 に答える
0

質問を完全に理解しているかどうかはわかりませんが、探しているのはエンジンオブジェクトで var this を定義していると思います..次に、関数で this の代わりにその変数を使用します..

すなわち:

var engine = function(){
var self = this;

receiveClickEvent = function(position)
{
    var funct = function(content)
    {
         **self**.respondToRequest(content); //<<<---- I want to refer engine's 'this'
    }
    requestByAsyncPost('request=click&position=['+position[0]+','+position[1]+')',   funct);
}

}
于 2012-11-04T15:05:20.397 に答える