简体   繁体   English

在 JavaScript 中访问外部 scope

[英]Accessing outer scope in JavaScript

So I've got this JS code here, and I'm trying to set obj from the success and error callbacks, but apparently the toLinkInfo function scope is not a parent scope of those?所以我在这里有这个 JS 代码,我试图从成功和错误回调中设置 obj,但显然toLinkInfo function scope 不是那些 Z31A1FD140BE4BEF2D11E121EC9A18 的父级? I always get null back from this function no matter what.无论如何,我总是从这个 function 得到 null。 I tried a bunch of stuff but couldn't get it to work, I guess I'm too used to C & friends:) How can I get this to work?我尝试了一堆东西,但无法让它工作,我想我已经习惯了 C 和朋友们:) 我怎样才能让它工作?

LinkInfoGrabber.prototype.toLinkInfo = function() {
    var obj = null;
    $.ajax({
        url: this.getRequestUrl(),
        success: function(raw) {
            obj = new LinkInfo(raw);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            obj = new LinkInfoException(jqXHR, textStatus, errorThrown);
        },
        dataType: 'html'
    });

    if (obj instanceof LinkInfo) {
        return obj;
    } else {
        throw obj;
    }
}

This code:这段代码:

if (obj instanceof LinkInfo) {
        return obj;
    } else {
        throw obj;
    }

runs immediately after you START the ajax call, but obj is not set until the ajax call finishes successfully.在您开始 ajax 调用后立即运行,但直到 ajax 调用成功完成后才会设置 obj。 This is a common misunderstanding.这是一个常见的误解。 The Ajax call is asynchronous. Ajax 调用是异步的。 Your call to $.ajax() starts the asynchronous call and then the rest of your function immediately executes.您对 $.ajax() 的调用会启动异步调用,然后您的 function 的 rest 会立即执行。 The success handler is called only when the ajax call succeeds (some time later).仅当 ajax 调用成功(一段时间后)时,才会调用成功处理程序。 You can't return obj from your function.您不能从 function 中返回 obj。 You have to handle obj in your success handler and then call anything further that wants to use it from the success handler.您必须在成功处理程序中处理 obj ,然后从成功处理程序中进一步调用任何想要使用它的东西。

That's because AJAX calls are asynchronous -- they happen at a different time from the rest of the context.这是因为 AJAX 调用是异步的——它们发生在与上下文的 rest 不同的时间。

Try giving it a callback function( called callback below ).尝试给它一个回调函数(下面称为回调)。

LinkInfoGrabber.prototype.toLinkInfo = function(callback) {
    $.ajax({
        url: this.getRequestUrl(),
        success: function(raw) {
            callback( new LinkInfo(raw) );
        },
        error: function(jqXHR, textStatus, errorThrown) {
            obj = new LinkInfoException(jqXHR, textStatus, errorThrown);
        },
        dataType: 'html'
    });
}

var l = new LinkInfoGrabber()
l.toLinkInfo(console.log) //replace console.log with something meaningful

While this approach does not provide exactly the same result as being able to call everything inline, it has the benefit of allowing for the asynchronous nature of the web.虽然这种方法不能提供与内联调用所有内容完全相同的结果,但它的好处是允许 web 的异步特性。

You're returning obj before the call to ajax returns, so it's not set to anything but null when it is returned.您在对ajax的调用返回之前返回obj ,因此在返回时它没有设置为 null 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM