1

The question is pretty simple but seems like this is impossible with the current implementation of Object.observe. Imagine :

var obj = { foo: 1 }
Object.observe(obj, function (changes) {
    console.log(changes)
})
obj.foo = 2

This will log an object with the changes I've made, as expected. So the question is how can I change the property without this side effect?

Jeremy Knees
  • 640
  • 1
  • 7
  • 18

1 Answers1

0

Well, one of the goals of Object.observe design is to catch absolutely all changes of the object, any time.

Answer depends on what you actually need. Code below, for example, will not cause change events on the object:

var obj = { foo: [1] }
Object.observe(obj, function (changes) {
    console.log(changes)
})
obj.foo[0] = 2

As technically object itself will not change.

One of possible options is to provide your own, patched Object.observe implementation that will notify observers only when some flag is on.

Or instead, to use some higher level idiom like pubsub mechanism where you can manage 'change' events emissions.

c-smile
  • 24,546
  • 7
  • 54
  • 79