I have an object (Thing) that keeps track of some value, as well as a set of conditions:
var Thing = function(currentValue) {
this.currentValue = currentValue;
this.conditions = [];
};
Conditions can be added to this list:
Thing.prototype.addCondition = function(condition) {
this.conditions.push(condition);
}
I want conditions to take the form of some function such that I could do
thing.addCondition(thing.exampleCondition(valueToCheckFor));
and these conditions could all be checked via
Thing.prototype.evaluateConditions = function() {
this.conditions.forEach(function(condition) {
if (condition()) {
//Do something
}
});
};
Currently I have one such condition function:
Thing.prototype.exampleCondition = function(value) {
return function() {
return this.currentValue === value;
};
};
This obviously doesn't work - this.currentValue is undefined within the anonymous function. My problem is that I need the value passed into exampleCondition to be evaluated against currentValue's value at the time that evaluateConditions() is invoked - therefore I can't do
Thing.prototype.exampleCondition = function(value) {
var c = this.currentValue;
return function() {
return c === value;
};
};
I'm kind of a javascript noob, but hopefully you brilliant people can point me in the right direction here.