Обнаружение изменений в массиве Javascript с использованием объекта Proxy
Относительно тривиально наблюдать за изменениями в массиве в 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(", "));
});
});
Однако, я недавно прочитал, что Array.observe
устарел, и мы должны использовать прокси-объект .
Как мы можем обнаружить изменения в массиве объекта Proxy? Я не могу найти каких-либо примеров, кого интересует разработка?
Ответы
Ответ 1
Из того, что я могу прочитать на странице
var arrayChangeHandler = {
get: function(target, property) {
console.log('getting ' + property + ' for ' + target);
// property is index in this case
return target[property];
},
set: function(target, property, value, receiver) {
console.log('setting ' + property + ' for ' + target + ' with value ' + value);
target[property] = value;
// you have to return true to accept the changes
return true;
}
};
var originalArray = [];
var proxyToArray = new Proxy( originalArray, arrayChangeHandler );
proxyToArray.push('Test');
console.log(proxyToArray[0]);
// pushing to the original array won't go through the proxy methods
originalArray.push('test2');
// the will however contain the same data,
// as the items get added to the referenced array
console.log('Both proxy and original array have the same content? '
+ (proxyToArray.join(',') === originalArray.join(',')));
// expect false here, as strict equality is incorrect
console.log('They strict equal to eachother? ' + (proxyToArray === originalArray));
Ответ 2
Вы можете сделать что-то вроде этого
new Proxy([], {
get(target, prop) {
const val = target[prop];
if (typeof val === 'function') {
if (['push', 'unshift'].includes(prop)) {
return function (el) {
console.log('this is a array modification');
return Array.prototype[prop].apply(target, arguments);
}
}
if (['pop'].includes(prop)) {
return function () {
const el = Array.prototype[prop].apply(target, arguments);
console.log('this is a array modification');
return el;
}
}
return val.bind(target);
}
return val;
}
});