Following MDN : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
If not in strict mode 'this' in a function will point to the global object.
However When trying to modify global variable in a function it does not work as I imagined. Is there some explanation to this or a spec one can refer to?
// this.somevariable = 'this is in global var'; // will not be in Global
somevariable = 'this is in global var'; // will make it to global
function something() {
somebar = 'foo'; // this will not change the global variable
this.somebar = 'foo'; // this will not change the global variable
console.log(this.somevariable);
}
something();
console.log( this.somebar ); // somebar undefined
P.S I am just trying to figure out how 'this' keyword works. I understand that modifying global variables is a bad idea as well as not using strict mode.
*Running in node v10.14