Where are objects declared through var in node environment mounted?

clipboard.png

are objects declared through var in the node environment mounted to the global global object? But through global., How can the result printed out by the variable name be undefined?
ask the boss for help?

var age = 22;
var printAge = function (age) {
    console.log(age);
}
console.log("var",window.age===22);//false
console.log("var",window.printAge===printAge);//false
console.log(global);//window
console.log(global.age);//undefined

if you write like this, you will no longer mount age on global

you need to write:

// var
age = 22;
var printAge = function(age) {
  console.log(age);
};

console.log(global.age); //22

(function(exports, require, module, filename, dirname)){
   //
}

that's because the code you execute is wrapped in a function, and var is at the top of the function scope. If you are going to the node instruction window to write this code, it is on global .


this var belongs to the module scope and is only valid within the current js file, not global. To achieve global, you need global.age = 1


are you sure this code will not report an error?

if the running environment is node, then there is no window at all

if the running environment is browser, then there is no global at all

Menu