Javascript 数组作用域 - 这里是新手

发布于 2024-08-26 17:43:39 字数 767 浏览 4 评论 0原文

所以,我一边学习 Javascript,一边玩白色的 Google Calendar API,我只是不明白这段代码是如何工作的:

var entriesResult = [];
var data = new Date(2010,3,22,17,0,0);
var callback = function(result) {       
    var entries = result.feed.getEntries();    
    if (entries.length != 0) {
        entriesResult = eventsManager(entries, 0, data);
        window.alert("inner entriesResult " + entriesResult.length);
    }
}
this.service.getEventsFeed(this.query, callback, handleGDError);
window.alert("outer entriesResult " + entriesResult.length);

eventsManager() 是一个返回对象数组的函数。

getEventsFeed() 它是一个 API 函数:它查询服务并将“提要根”(包含所选项目的提要)传递给回调函数。

为什么第一个警报(内部..)输出有效的entriesResult.length,而第二个警报(外部..)始终输出0?

我认为 javascript 数组总是通过引用传递,我的代码有什么问题吗? 谢谢 :)

So, I am learning Javascript while playing white Google Calendar APIs and I just can't figure how this piece of code is working this way:

var entriesResult = [];
var data = new Date(2010,3,22,17,0,0);
var callback = function(result) {       
    var entries = result.feed.getEntries();    
    if (entries.length != 0) {
        entriesResult = eventsManager(entries, 0, data);
        window.alert("inner entriesResult " + entriesResult.length);
    }
}
this.service.getEventsFeed(this.query, callback, handleGDError);
window.alert("outer entriesResult " + entriesResult.length);

eventsManager() is a function that returns an array of Objects.

getEventsFeed() it's an API function: it queries the service and pass a "feed root" (a feed with selected items) to the callback function.

Why the first alert (inner..) outputs a valid entriesResult.length while the second one (outer..) always outputs a 0?

I tought javascript arrays are always passed by reference, what's wrong whit my code?
Thank you :)

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

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

发布评论

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

评论(2

乱世争霸 2024-09-02 17:43:39

getEventsFeed 函数进行异步 AJAX 调用,并在服务器回复时调用callback。换句话说,回调函数在其余代码之后运行一段时间。

因此,当数组仍为空时,外部 alert 在回调之前执行。

编辑

要从 AJAX 调用返回值,您需要接受回调作为参数,然后在有要返回的值后调用回调。

例如:

function myFunction(someParam, callback) {
    //Do things...
    var eventsFeedCallback = function() { 
        //Do more things, and figure out what to return
        callback(someValue);  //Call the user's original callback to give back a value
    };
    this.service.getEventsFeed(this.query, eventsFeedCallback , handleGDError);  
    //Do more things...
}

您可以像调用 getEventsFeed 一样调用此函数。
例如:

myFunction("Parameter!", function(value) {
    //This is the callback, and will be called when the AJAX call finishes
    //Do things with value
});

The getEventsFeed function makes an asynchronous AJAX call and calls callback when the server replies. In other words, the callback function is run some time after the rest of the code.

Therefore, the outer alert is executed before the callback, when the array is still empty.

EDIT

To return a value from an AJAX call, you need to accept a callback as a parameter, then call the callback once you have a value to return.

For example:

function myFunction(someParam, callback) {
    //Do things...
    var eventsFeedCallback = function() { 
        //Do more things, and figure out what to return
        callback(someValue);  //Call the user's original callback to give back a value
    };
    this.service.getEventsFeed(this.query, eventsFeedCallback , handleGDError);  
    //Do more things...
}

You would call this function the same way you call getEventsFeed.
For example:

myFunction("Parameter!", function(value) {
    //This is the callback, and will be called when the AJAX call finishes
    //Do things with value
});
汐鸠 2024-09-02 17:43:39

该行:

 window.alert("outer entriesResult " + entriesResult.length); 

在启动异步操作后(完成之前)立即执行。

回调函数中的行稍后在异步操作完成后执行。

The line:

 window.alert("outer entriesResult " + entriesResult.length); 

is executed immediately after you start the async operation (before it completes).

the line in the callback function is executed later, after the async operation is complete.

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