I have some functions that only return values if some underlying 'stuff' in the dom has changes, as so:
function getFoo() {
if (typeof this.prev === "undefined") {
this.prev = null;
}
// calculate x
if (x != this.prev) {
this.prev = x;
return this.prev;
} else {
return;
}
}
foo returns none if the computer var x hasnt changed. This seems to work fine when called from the console.
Next, I wrapped this in an object so i could sequentially call similar functions:
function bar () {
this.plugins = {
foo: getFoo
};
for (var attr in this.plugins) {
if (this.plugins.hasOwnProperty(attr)) {
console.log(this.plugins[attr]());
}
}
The strange thing is that when I call bar() now, the underlying functions (such as getFoo) always return values -- even if the underlying stuff in the dom hasnt changed. it seems that the functions are destroyed upon use (in the plugin object) -- how can i persist these?
returnwill returnundefined.thishas in all these functions. And I would bet my bottom dollar that it's athisscope issue. Try changing the invoking of those function to.call(this)