0

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?

4
  • You can't, you need infrastructure to invoke callbacks when values change Commented Jan 15, 2012 at 21:21
  • Just call the function instead of setting the flag. Commented Jan 15, 2012 at 21:21
  • @Raynos: No; he can use properties Commented Jan 15, 2012 at 21:21
  • Is the flag the property of an object? You could use a setter... Commented Jan 15, 2012 at 21:23

1 Answer 1

3
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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.