I have a flag , named 'flag', and when this flag get's a specific value , I want to invoke a specific function. anyone knows how to do that?
-
You can't, you need infrastructure to invoke callbacks when values changeRaynos– Raynos2012-01-15 21:21:05 +00:00Commented Jan 15, 2012 at 21:21
-
Just call the function instead of setting the flag.SLaks– SLaks2012-01-15 21:21:08 +00:00Commented Jan 15, 2012 at 21:21
-
@Raynos: No; he can use propertiesSLaks– SLaks2012-01-15 21:21:26 +00:00Commented Jan 15, 2012 at 21:21
-
Is the flag the property of an object? You could use a setter...thejh– thejh2012-01-15 21:23:47 +00:00Commented Jan 15, 2012 at 21:23
Add a comment
|
1 Answer
var EventEmitter = require("events").EventEmitter;
var flags = Object.create(EventEmitter.prototype);
Object.defineProperty(flags, "someFlag", {
get: function () {
return this._someFlag;
},
set: function (v) {
this._someFlag = v;
this.emit("someFlag", v);
}
});
flags.on("someFlag", callback);