Search code examples
javascriptobject.observe

JavaScript Object.Observe delegation


I have JavaScript array which is consists of many objects.

var array=[{name:1},{name:2}.....]

I want to use Object.observe to handle data changes on each of these objects.

Is there any way to apply something like event delegation here , in order to not apply observe for each of objects?

If I use Object.observe for array itself than changing its items doesn't trigger anything.


Solution

  • No, that's not possible.

    Imagine code like this:

    var foo = {...};
    var bar = [foo, {..}, {..}, {..}]
    Object.yourWeirdMagicObserve(bar, ..);
    

    Would changing foo trigger your callback now? Or only modifications to bar[0]? When you use bar[0] the object is retrieved from bar but besides that it has no association with bar whatsoever.