Skip to content

Instantly share code, notes, and snippets.

@Drag13
Created September 20, 2018 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drag13/a4e6d2601060355c0205b4fbb4cad07d to your computer and use it in GitHub Desktop.
Save Drag13/a4e6d2601060355c0205b4fbb4cad07d to your computer and use it in GitHub Desktop.
function spyFactory(array) {
const interceptor = {
get: function (obj, prop) {
if (prop !== 'push') { return obj[prop]; }
return function(...args) {
console.log('prepush');
Array.prototype.push.apply(this, args);
console.log('postpush');
}
}
}
return new Proxy(array, interceptor);
}
const spyedArray = spyFactory([77, 5]);
console.log(spyedArray[0]);
spyedArray.push('4');
console.log(spyedArray[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment