Search code examples
javascriptecmascript-harmonyobject.observe

Object.observe - Multiple observers support?


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?


Solution

  • 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"