Manuel
Manuel

Reputation: 9522

Node.JS bind Events to Variables?

Is it possible in node to bind events to variables? Eg having an array and trigger an callback function if the array length falls bellow 10.

Upvotes: 2

Views: 292

Answers (1)

Scott Puleo
Scott Puleo

Reputation: 3684

If you are running Node >= 0.11.13 you can use the ES6 feature Object.observe.

var nums = [1,2,3,4,5,6,7,8,9,10];

Array.observe(nums,function(changes) {  
  if(nums.length < 10) {
    console.log('Array nums has less than 10 elements!');
  }
});

var popped = nums.pop();

If running the unstable branch is not your thing you can use one of the many polyfills / shims:

npmjs.org

Upvotes: 3

Related Questions