1

次の js スニペットがあります。

<script language="javascript">
          function test() {
            this.a = 10;
        };
        test.prototype.next = function () {
            alert('before: ' + this.a);
            $.ajax({
                url: 'some-url',
                async: true,
                type: 'POST',
                dataType: 'json',
                success: this.callback

            });
        };
        test.prototype.callback = function () {
            alert('after: ' + this.a);
        };

        $(document).ready(function () {
            var test1 = new test();
            test1.next();
        });
      </script>

それは常に結果を生成します: before: 10 after: undefine.

$.post のコールバック関数で、class のプロパティが定義されていないのはなぜですか。誰でも私を助けることができますか?どうもありがとう。

4

5 に答える 5

7
success: this.callback

これはうまくいきません。コールバックをインスタンス ( ) にバインドする必要がありますthis。そうでなければ、その範囲を失ってしまいます。関数だけでは、残念ながらそれを覚えていません。

手動の方法は

var me = this;

// .....

success: function(x,y,z) { me.callback(x,y,z) }

jQueryがこれに備えている組み込みのヘルパーが今のところ見つかりません。CoffeeScript では、太い矢印を使用するだけです=>

更新: jQuery の方法が見つかりました:contextに対して指定できるパラメーターがあり$.ajax、それはthisすべてのコールバックに対してなります:

 success:  this.callback,
 context:  this
于 2011-11-21T07:05:23.690 に答える
1

コンテキストが失われているためthis、別のオブジェクトを指しています。jQuery で使用できる組み込みのプロキシ関数があります。コードを次のように変更します。

// ...
$.ajax({
    url: 'some-url',
    async: true,
    type: 'POST',
    dataType: 'json',
    success: $.proxy(this.callback, this)
});
// ...
于 2011-11-21T07:19:00.757 に答える
0

次のようなウルコードのいくつかの変更は行います

<script language="javascript">
          function test() {
            this.a = 10;
        };
        test.prototype.next = function () {
            alert('before: ' + this.a);
            var oThis = this;          //Save refernce of Test() function in a varaiable. then use it 
            $.ajax({
                url: 'some-url',
                async: true,
                type: 'POST',
                dataType: 'json',
                success: oThis.callback // This is change this=> earlier was referring 
                                        //$.ajax object so save this of test() 
                                        // in a variable and then use it 
            });
        };
        test.prototype.callback = function () {
            alert('after: ' + this.a);
        };

        $(document).ready(function () {
            var test1 = new test();
            test1.next();
        });
      </script>
于 2011-11-21T08:09:08.920 に答える
0

問題は、関数の閉鎖によるものです。コールバックの「this」は「test」ではなく、コールバック関数自体を指しています。

このようなものを使用してください

function binder (func, thisValue) {
            return function () {
                return func.apply(thisValue, arguments);
            }
        }

var bindedFun = binder(function () {
            alert('after: ' + this.a);
        }, test);
于 2011-11-21T07:07:07.537 に答える
0

これを行うもう 1 つの方法は、コールバックをローカル スコープにアクセスできるローカル関数にし、参照をローカル スコープに保存することthisです。コールバックは へのアクセスを必要とする可能性があるため、これはうまく機能すると思いますがthis、必ずしもそれを行うためのメソッドである必要はありません。

    test.prototype.next = function () {
        var self = this;

        function callback() {
            alert('after: ' + self.a);
        }

        alert('before: ' + this.a);
        $.ajax({
            url: 'some-url',
            async: true,
            type: 'POST',
            dataType: 'json',
            success: callback

        });
    };
于 2011-11-21T08:05:33.193 に答える