我想Proxy
在一个名为的自定义类上使用,ObservableList
其中包含一个Array
. 由于Proxy
仅在 ES6 之后可用,我想知道是否有任何替代实现。
我的要求是一旦发生变化就让观察者得到更新(而不是被注意到)ObservableList
,这样观察者总是与带有某种过滤或映射方法的可观察者保持一致。
var activities = new ObservableList(['reading', 'swimming']);
var sAct = activities.filter(function(v) {
return v[0] === 's';
});
// expect sAct.list to be ['swimming']
var meAct = activities.map(function(v) {
return 'I am ' + v;
});
// expect meAct.list to be ['I am reading', 'I am swimming']
activities.list.push('smiling');
console.log(sAct.list, meAct.list);
// expect sAct.list to be ['swimming', 'smiling']
// expect meAct.list to be ['I am reading', 'I am swimming', 'I am smiling']
activities.list[1] = 'snoopying';
console.log(sAct.list, meAct.list);
// expect sAct.list to be ['swimming', 'snoopying']
// expect meAct.list to be ['I am reading', 'I am snoopying', 'I am smiling']