Open fisker112 opened 7 years ago
Hey Fisker,
You asked this a while ago but hopefully, this helps you or possibly someone else.
This one gave me a lot of trouble as well. I also didn't see how the Spy class could track a function outside of its scope, unless... you augment the function you're spying on to update a property of the Spy class.
return originalFunction.apply(this, arguments)
This refers to Spy. OriginalFunction is what was just updated when you assigned the new function you created to target[method]. Because you've changed the context of the function with apply the function is now within Spy's context.
Long story short your augmented method now has access to the Spy property count, well more specifically, the result object that holds count.
I am stuck on 12th exercise of the workshop. Can someone please explain the logic behind the solution? I mean how can the function Spy track another function that will be called outside of itself? Especially this part:
// replace method with spy method target[method] = function() { result.count++ // track function was called return originalFunction.apply(this, arguments) // invoke original function
Thanks a lot in advance.