Variable scoping and the jQuery.getJSON() method

Posted by jerome on Stack Overflow See other posts from Stack Overflow or by jerome
Published on 2010-04-07T18:36:38Z Indexed on 2010/04/07 18:53 UTC
Read the original article Hit count: 515

Filed under:
|
|

The jQuery.getJSON() method seems to ignore the normal rules of scoping within JavaScript.

Given code such as this...

someObject = {
    someMethod: function(){
        var foo;

        $.getJSON('http://www.somewhere.com/some_resource', function(data){
            foo = data.bar;
        });

        alert(foo); // undefined
    }
}

someObject.someMethod();

Is there a best practice for accessing the value of the variable outside of the getJSON scope?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about getjson