Synchronizing loading js files with ajax calls and loading js files with – Alex Pacurar Oct 28 '10 at 06:06

1 Answers1

0

I am not sure why CASE 1 happened to work for you, but your problem seems to lie here:

$.getJSON('someurl', function(data)
{
    alert(core === this); // <----- false, 'this' refers to the function's scope
    for(key in this.all)
        alert(key);
});

"this" in javascript isn't the same as "this" in C++ or Java

The problem is that function(data){...} creates a closure (with a new this). So, inside that function, this no longer refers to core, it refers to the scope of the context from which it was called (somewhere inside jQuery in this case). You can fix it by protecting this with a new variable, like so.

var self = this;
$.getJSON('someurl', function(data) {
    for(var key in self.all){
        alert(key);
    }
});

This question can point you in the right direction for demystifying the this keyword in javascript.

Community
  • 1
  • 1
skabbes
  • 890
  • 7
  • 17