Given the following code snippet.
var name = 'John';
function foo() {
console.log(this.name);
}
foo();
Why is it that when I run this code in the browser the log outputs the name, but when I run this same code snippet in node it outputs undefined
?
In the browser this
refers to the window object, and the global variable will get attached to the window. Now in node this
will refer to the global object in this example, so does my global variable not get attached to the global object as it does in the browser when it gets attached to the window?