我有一个带有观察者函数的列表:
var names = ['joe', 'bob', 'loic'];
Object.observe(names, function(changes){
changes.forEach(function(change) {
console.log(change.type, change.name)
});
console.log("names",names);
});
console.log("######## del bob");
names.splice(1,1);// output is "update 1 - delete 2", why?
console.log("names", names) // but "bob" is the one deleted in the list
根据change对象删除的索引是2,但是我删除了索引1,列表实际上删除了索引1。你知道为什么吗?
是因为索引1被更新得到索引2的值,而索引2被删除了吗?有没有办法获得实际删除的元素索引?
发布于 2014-11-28 14:38:43
您可以使用Array.observe在正确的索引处捕获拼接事件:
var names = ['joe', 'bob', 'loic'];
Array.observe(names, function(changes){
changes.forEach(function(change) {
console.log(change.type, change.name);
});
});
感谢@xavier-delamotte :)
https://stackoverflow.com/questions/27175764
复制相似问题