“这个”是什么意思?参考下面的javascript?

发布于 2024-11-17 21:23:06 字数 1051 浏览 2 评论 0原文

免责声明:我问的是 this 的具体用途,而不是 this 的一般用途。因此,请不要使用 google/copy/paste 速度答案(:

我有下面的 javascript/jquery 代码:

var req = {};

function getData()
{
    var fromPage = 0;
    var toPage = 1;

    req = $.ajax({
                    url: "/_vti_bin/lists.asmx",
                    type: "POST",
                    dataType: "xml",
                    data: xmlData,
                    complete: onSuccess,
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert("error: " + xhr.statusText);
                        alert(thrownError);
                    },
                    contentType: "text/xml; charset=\"utf-8\""
                });

     req.fromPage = fromPage;
     req.toPage = toPage;
}

function onSuccess(resp) {
    alert(this.fromPage);
}

我发现代码在两个地方使用 fromPage 非常令人困惑(抱歉不是我的代码)。

this 是指在 getData 中声明的 var 还是作为 req 对象一部分的变量?或者可能完全是其他东西...... 。

任何帮助表示赞赏

DISCLAIMER: I am asking about a specific use of this, not what this is used for in general. So, please no google/copy/paste speed answers (:

I have the javascript/jquery code below:

var req = {};

function getData()
{
    var fromPage = 0;
    var toPage = 1;

    req = $.ajax({
                    url: "/_vti_bin/lists.asmx",
                    type: "POST",
                    dataType: "xml",
                    data: xmlData,
                    complete: onSuccess,
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert("error: " + xhr.statusText);
                        alert(thrownError);
                    },
                    contentType: "text/xml; charset=\"utf-8\""
                });

     req.fromPage = fromPage;
     req.toPage = toPage;
}

function onSuccess(resp) {
    alert(this.fromPage);
}

I find it pretty confusing that the code is using fromPage in two places (sorry not my code).

Does this refer to the var declared inside of getData or the one that is part of the req object? Or maybe something else entirely....

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱她像谁 2024-11-24 21:23:06

根据 jQuery.ajax 文档:

所有回调中的 this 引用是设置中传递给 $.ajax 的上下文选项中的对象;如果未指定上下文,则这是对 Ajax 设置本身的引用。

换句话说,由于您没有设置 context 选项,因此 this 将是作为参数传递的选项对象 {...}$.ajax

您发布的代码似乎是错误的:它从错误的对象读取 fromPage 。如果您在选项对象上设置 fromPage ,它就会起作用:

req = $.ajax({
    //other options here...
    fromPage: fromPage
});

According to jQuery.ajax documentation:

The this reference within all callbacks is the object in the context option passed to $.ajax in the settings; if context is not specified, this is a reference to the Ajax settings themselves.

In other words, since you didn't set the context option, this will be the options object {...} passed as the parameter to $.ajax.

The code you posted seems wrong: it reads fromPage from the wrong object. It would work if you set fromPage on the options object instead:

req = $.ajax({
    //other options here...
    fromPage: fromPage
});
思慕 2024-11-24 21:23:06

onSuccess() 正在 ajax 请求上下文中的 complete 处理程序中调用,该处理程序被分配给 req 对象,因此 < code>this 是上下文 - 即 req 对象,而 fromPage 实际上是 req.fromPage

onSuccess() is being called from the complete handler in the context of the ajax request, which is being assigned to the req object, so this is that context - i.e. the req object, and fromPage is in fact req.fromPage

风蛊 2024-11-24 21:23:06

我相信 this 指的是 req.fromPage 因为它是包含被调用函数的对象。

I belive this is refering to req.fromPage since that is the object that contains the called function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文