Javascript Array Scope - newbie here

Posted by Gianluca on Stack Overflow See other posts from Stack Overflow or by Gianluca
Published on 2010-03-21T16:05:26Z Indexed on 2010/03/21 16:11 UTC
Read the original article Hit count: 423

Filed under:
|
|

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 :)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about arrays