Created
September 20, 2018 16:33
-
-
Save Drag13/a4e6d2601060355c0205b4fbb4cad07d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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