0

While trying to understand scoping and the setting of the 'this' context, I am having a bit of a hard time to understand how the context gets set when returning the following anonymous function. Can somebody please help me understand why the this inside the returned anonymous function gets set to the resize event object? Also, instead of the resize event, had I executed the function within the callback of a user defined object's member function, would the this value then be that user defined object? I can't seem to be able to wrap my head around this. Any tips and help would be appreciated.

function randomFunction(fn) {
   return function() {
      var context = this;
      var args = arguments;
      fn.apply(this, arguments)
   }
}
window.addEventListener('resize' , function(){
    randomFunction(function(){console.log("log to console");})
})
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Soham Bhaumik
  • 211
  • 2
  • 15

0 Answers0