In a client-side only app, I'm loading two js files with script tags, one after another, and getting errors as appear in the comments below:
obj.js
'use strict';
let obj = {};
obj.func1 = () => {
return 'stuff';
}
obj.func2 = (arr) => {
debugger; // in devtools, value of 'this' is obj, as expected
console.log(this); // clicking 'next' button in devtools,
// the object that is actually being printed to
// console is the window object!!!
// javascript, wtf?!?!
debugger;
let myStuff = this.func1(); // fails because window has no 'func1' property
...
...
}
window.obj = obj;
script.js
'use strict';
obj.func2()
// Uncaught TypeError: this.func1 is not a function
Why in the world does it happen? Why there's a difference in 'this' value between devtools debugger and actual result in the browser?
Edit:
See in the screentshot below how, when I'm executing console.log(this)
myself in the console while being paused by the debugger
break point, 'this' is Object(), and one step next (believe me that it's exactly one step next), window object is being printed to console.