I am currently learning JavaScript and I thought that I understood functions. Apparently not. I thought that, in the following code, first argument of apply() call, which is this, would evaluate to global object.
var x = 100;
function trace(o, m) {
let original = o[m];
o[m] = function(...args) {
console.log(new Date(), "Entering:", m);
let result = original.apply(this, args);
console.log(new Date(), "Exiting:", m);
return result;
};
}
let obj = {
x: 10,
m: function() {
console.log('X is: ', this.x);
}
}
trace(obj, 'm');
obj.m();
Can somebody clarify why that's not the case?