0

Good day, friends.

I use module pattern and faced a problem when THIS inside the method points to WINDOW instead of object. here is the example:

window.Module = (function () {
var _private = '';

var _helper = {
    some: function() {
        _private = setTimeout(this.someCallback, 1000)
    },
    someCallback: function() {
        console.log(this);
    }
}

var _module = {
    init: function() {
        _helper.some();
    }
};

return _module;
})();
Module.init();

As u see Module.init(); returns Window. Can u guys please explain what exactly is happening in this example, because i googled about it and read about some similar issues with no luck to understand...

user1128677
  • 467
  • 2
  • 10
  • 17
  • 2
    [Here's another question on the topic.](https://stackoverflow.com/questions/9604929/javascript-module-pattern-scope-and-this?rq=1) – Pointy Mar 19 '19 at 13:49
  • 1
    Oh, now i understand, `setTimeout` running with THIS pointing to global scope, so something like this `_private = setTimeout( () => { this.someCallback() }, 1000)` made a trick – user1128677 Mar 19 '19 at 14:01

0 Answers0