开发者

Global variables, Javascript

开发者 https://www.devze.com 2022-12-08 17:13 出处:网络
Can I do this ? var global = 0; var truc = $.getJSON(\"events.json\", function(json){ //alert(\"J开发者_C百科SON Data: \" + json[0].titre);

Can I do this ?

var global = 0;

var truc = $.getJSON("events.json", function(json){
 //alert("J开发者_C百科SON Data: " + json[0].titre);
  global = json;
});

I want to keep the contents of json and work with it outside the function. If it was C, I would just keep the pointer but I don't know what to do with JS


yes you can do that


I don't know the details on how json works, so I cannot say what happens in your case, but this simple test works as a simplified example on how your global variable will actually work:

var global = 0;

function callTest(arr) {
    //alert("JSON Data: " + json[0].titre);
    global = arr;
}

var array = new Array("w", "q");
callTest(array);
alert(global);

This means that it has something to do with how json works. One thing: Are you sure the function initialized with json is actually run before you test with alert(global) ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号