English 中文(简体)
在我的案件中如何正确使用 Java字?
原标题:How to correctly use Javascript this in my case?

I have a following (simplified) code:

var myModule = {

    submitDummyForm: function(){ 
        console.log(this); // the right object is logged out
        var that = this; // keep a reference
        $.ajax({
            type: POST ,
            url:  http://localhost/ ,
            data: {dummyData:  something },
            dataType:  json ,
            success: that.dummyFormSuccess

        });
    },

    dummyFormSuccess: function(data){ 
        console.log(this); //  this  is logged out as some foreign object, most probably jQuery.ajax object
    }
}

这导致德米Form Success失去这一机会,无论我是否利用这一机会,也无论我是否把dummyForm Successfor作为我的jaxSubmitForm()的理由。

但是,根据我的需要,执行以下法典:

var myModule = {

    submitDummyForm: function(){ 
        console.log(this); // the right object is logged out
        var that = this; // keep a reference
        $.ajax({
            type: POST ,
            url:  http://localhost/ ,
            data: {dummyData:  something },
            dataType:  json ,
            success: function(data) {
                 that.dummyFormSuccess(data);
            }
        });
    },

    dummyFormSuccess: function(data){ 
       console.log(this); // now  this  is logged out correctly as the real myModule object
    }
}

我仍然对 Java稿的先进专题感到不舒服,但我已经知道,这可能会根据使用地点重新定义。 我认为,如果我利用这一手段来储存这一提法,那么我也应当把这一点放在所谓的职能中。 我可以这样说,这似乎令人不快。 总结性功能中的“mmyForm Success(数据)”是正确的,但是,如果我只把它分配给“jax”成功,我就失去了这一成功。

没有人能够解释,在我的案件中,这是错失的,为什么在第二个例子中发挥作用? 难道这在我的案件中是个问题,还是仅仅一个语言的特征?

最佳回答

一切都是正确的。 页: 1 因此,这种方式深深扎根于 j里,它称作ajax.success。 因此,this 上载ajax

采用第二种做法,你设立了匿名职能,将其编为<条码>。 因此,在您的匿名职能中,this points to ajaxbject, but :>ps is accessible and have not been over written.

问题回答

页: 1

The value of this parameter is determined by the callsite – the code that calls your function.
When you pass that.dummyFormSuccess or this.dummyFormSuccess, you re just passing a function that happens to come from your object.
The this or that object is just used to retrieve the function instance; it isn t bundled with the function.

When jQuery calls your callback, it always calls it in the context of the jqXHR object.
When you pass function(data) { that.dummyFormSuccess(data); }, your function expression is called in the context of this jqXHR object.
However, your callback then calls dummyFormSuccess in the context of that, so its this is what you want it to be.

You can treat this in JavaScript as additional argument in function argument list. So each call of function will contain its own value in this argument. Here are all three ways of invoking function in JS

  • foo(); - inside the function this will be set to default namespace object (window in case of browser environment).

  • obj.foo(); - this将登上obj

  • foo. calls(obj); - foo will beed with this set to obj (与上)

由于一项功能也可指标(并且是标的), > 这里指该功能。 (不同范围)

您需要做的是:var, = ; out of dummyForm :Function (the line above it), 然后是console.log( that)

var that = this;
dummyFormSuccess: function(data){ 
   console.log(that); // now  this  is logged out correctly as the real myModule object
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.