I am quite puzzled about the workings of closures in this particular code snippet:
function Spy(target, method) {
var result = {count: 0},
oldFn = target[method];
target[method] = function(input) {
result.count++;
return oldFn.apply(target, arguments);
}
return result;
}
Now, when you assign it to a variable like this:
var logSpy = Spy(console, 'log')
The logSpy object contains a count property. If you were to invoke console.log, the overridden function would increase the value of result.count, which is enclosed within the function. But how exactly does the connection between the enclosed object and the global logSpy object work? My understanding is that the logSpy object refers to the enclosed object due to objects being passed as reference. So, can we say that logSpy technically doesn't exist in the global execution context but rather serves as a reference to a closure?