I have the following code:
var tradingInterface = function() {
this.json = '';
this.init = function() {
$.get( '/whatever',{}, function(data) {
this.json = data;
// Rebuilds Everything
this.rebuildAll();
});
};
this.rebuildAll = function() {
//whatever here
};
};
Why am I getting in init function the following error?
ReferenceError: this.rebuildAll is not defined
this.rebuildAll();
Why can I access to this.json without scoping problems but not to this.rebuildAll?
I wrote a similar previous thread but i was redirected to How to access the correct `this` / context inside a callback? but i am not able to make it work properly.
As thw thread suggets, I tried with:
var tradingInterface = function() {
this.json = '';
var self = this;
this.init = function() {
$.get( '/whatever',{}, function(data) {
this.json = data;
// Rebuilds Everything
self.rebuildAll();
});
};
this.rebuildAll = function() {
//whatever here
};
};
The error disappears but rebuildAll function is not doing what it should...
I need some help...
Regards,
this.json. You can always assign a property to an object (almost always). However, you are readingthis.rebuildAlland try to call it as a function. Sincethis.rebuildAllisundefined, you cannot call it. Assigning tothis.rebuildAllwould work fine as well (although of course it wouldn't what you wanted to do):this.rebuildAll = 42;.thisin the function references to thewindowobject and it does not have therebuildAllmethod.thisinside of the$.get....isn't actuallytradingInterface. To clear things up, makethis.json = 'test'and thenconsole.log(this.json)inside your$.get