在Javascript中观察数组中的更改是相对微不足道的.
我使用的一种方法是这样的:
// subscribe to add, update, delete, and splice changes
Array.observe(viewHelpFiles, function(changes) {
// handle changes... in this case, we'll just log them
changes.forEach(function(change) {
console.log(Object.keys(change).reduce(function(p, c) {
if (c !== "object" && c in change) {
p.push(c + ": " + JSON.stringify(change[c]));
}
return p;
}, []).join(", "));
});
});
Run Code Online (Sandbox Code Playgroud)
但是,我最近读过的Array.observe是不推荐使用的,我们应该使用代理对象.
我们如何检测Proxy对象中数组的变化?我无法找到任何有兴趣详述的例子吗?
我刚读过这篇文章:http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx
如果你在创建/ 时选择使用构造函数,我的印象是覆盖Object或Array只有效果,但是,根据那篇文章,它也对文字创建有影响(和)......arraysobjects{}[]
我的逻辑:
Array = function(){ alert('Hi'); };
[1,2,3,4,5];
([1,2,3,4,5]);
var a = [1,2,3,4,5];
// ...
// ... Nothing is alerted
Run Code Online (Sandbox Code Playgroud)
那么,我是疯了还是有一些我不知道的特定于实现的怪癖?