123qwe
123qwe

Reputation: 1585

How to handle Event of changing the object variable in JS || JQuery?

I have got variable e.g. var obj = { foo: 'fooval', bar: 'barval'} How to write event and trigger to detect any modification of obj and then call info in console like this: console.log('changed obj.key from oldVal to newVal');

Upvotes: 1

Views: 195

Answers (1)

Oleksandr T.
Oleksandr T.

Reputation: 77492

There is new feature Object.observe, NOTE - this feature supported only in Chrome

var obj = { foo: 'fooval', bar: 'barval'};

Object.observe(obj, function(changes) {
    console.log(changes);
});

obj.foo = 'bar';

Also there are polyfills, for example Object.observe

Upvotes: 4

Related Questions