0

I am using Object.observe() on node v0.11.13.

It looks like the time of the observation callback to be called can't be predicted. is it a bug or a feature?

Take a look at this code:

function observe(obj,name){
    Object.observe(obj, function(o){
        console.log(name,o);
    });
    return obj;
}
var boo = observe({foo:1},'a');
var doo = observe({foo:1},'b');
doo.foo=2;
boo.foo=2;

The output looks like:

a [ { type: 'update', object: { foo: 2 }, name: 'foo', oldValue: 1 } ]
b [ { type: 'update', object: { foo: 2 }, name: 'foo', oldValue: 1 } ]

I would expect an opposite order. I wonder if this is related to the spec or to node impl' of this feature.

4

1 回答 1

1

它似乎遵循观察者注册的顺序,而不是值更改的顺序。

var doo = observe({foo:1},'a');
var boo = observe({foo:1},'b');
var zoo = observe({foo:1},'c');
var too = observe({foo:1},'d');

zoo.foo = 2;
too.foo = 2;
doo.foo= 2;
boo.foo= 2;

a [Object]
b [Object]
c [Object]
d [Object]

这是有道理的,因为在通过执行堆栈处理的同步运行期间对同一对象的多个更改将被批处理在一起。

于 2014-12-25T18:45:43.083 回答