Reputation: 2770
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?
Upvotes: 0
Views: 69
Reputation: 816472
Well, when you call bar()
, it calls
this.plugin.getFoo();
so this
inside getFoo
will reference this.plugins
.
You initialize this.plugin
with a new object every time you call bar()
:
this.plugins = {
foo: getFoo
};
Honestly your structure seems to quite confusing. Do you know that this
inside the bar()
function refers to the window
object? You are "polluting" the global namespace with that. Same for getFoo
if you are calling it directly.
Maybe you should have more something like this:
var bar = {
plugins: {foo: getFoo},
run: function() {
for (var attr in this.plugins) {
if (this.plugins.hasOwnProperty(attr)) {
console.log(this.plugins[attr]());
}
}
}
};
the you can call:
bar.run();
Another way of having persistent and private data between function calls is to use a closure:
var getFoo = (function() {
var prev = null;
return function(x){
if (x != prev) {
prev = x;
return prev;
}
}
}());
Here the function return by the immediate function closes over prev
. No matter where you assign getFoo
it will have always access to prev
and it does not overwrite any other prev
variable in some other scope.
I think it is also better when you pass x
to the function.
Upvotes: 1
Reputation: 56
So everytime you call "bar()" you are overwriting the plugins attribute of your object. Just put a simple check in before redefining the plugins attribute, like this:
function bar () {
if (typeof this.plugins === "undefined") {
this.plugins = {
foo: getFoo
};
}
for (var attr in this.plugins) {
if (this.plugins.hasOwnProperty(attr)) {
console.log(this.plugins[attr]());
}
}
...
Upvotes: 1