0

Can someone tell me if Object.observe() will (once implemented) allow to register multiple observers via multiple calls? Or only the last one will stay?

For example if I do:

Object.observe(obj, myFirstFunction);
Object.observe(obj, mySecondFunction);

Will both functions get called when my object is updated or only mySecondFunction?

Cœur
  • 32,421
  • 21
  • 173
  • 232
flawyte
  • 7,678
  • 4
  • 44
  • 63

1 Answers1

1

I tried this and I got that both are being called.

var obj = {name:'MD'};
var myFirstFunction = function (changes) {
    console.log("First " ,Object.keys(changes[0]))
}
var mySecondFunction = function (changes) {
    console.log("Second ",Object.keys(changes[0]))
}

Object.observe(obj, myFirstFunction);
Object.observe(obj, mySecondFunction);

obj.name = "Mritunjay"
Mritunjay
  • 22,738
  • 6
  • 47
  • 66
  • How did you tried ? I enabled Google Chrome's *Experimental JavaScript* flag but when I write `Object.observe` in my console it outputs `undefined`. I have Chrome 35 and according to [this](https://kangax.github.io/compat-table/es7/) table it should be available. Any clue ? – flawyte Jul 17 '14 at 13:55
  • I tried it in normal chrome console, it was not working, then I tried with node. Same error in chrome. Trying – Mritunjay Jul 17 '14 at 13:57
  • 1
    @Flawyte see [here](http://blog.chromium.org/2014/04/chrome-35-beta-more-developer-control.html) it says `Update April 29th: Object.observe has been pushed back to Chrome 36.` – Mritunjay Jul 17 '14 at 14:04