简体   繁体   English

对象。观察顺序

[英]Object.observe order

I am using Object.observe() on node v0.11.13. 我在节点v0.11.13上使用Object.observe()。

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. 我想知道这与规范或该功能的节点含义有关。

It seems to be following the order in which observers are registered rather than the order in which values are changed. 它似乎遵循注册观察者的顺序,而不是更改值的顺序。

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]

That makes sense, because multiple changes to the same object during a synchronous run through execution stack processing will be batched together. 这是有道理的,因为在同步执行过程中,对同一对象的多次更改将被批处理在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM