node. JS Global Object
There is a special object in JavaScript called a global object, and all of its properties can be accessed anywhere in the program, that is, global variables.
In browser JavaScript, the window is usually a global object, while the global object in node. JS is global, and all global variables (except the global itself) are properties of the global object.
In node. js, we can directly access the properties of global without having to include it in the app.
Global objects and global variables
The fundamental role of global is to be the host of variables. As defined by ECMAScript, variables that meet the following conditions are global variables
Variables defined at the outermost layer
Properties of global objects
Implicitly defined variables (variables that do not define a direct assignment)
When you define a global variable, the variable also becomes a property of the global object, and vice versa. It is important to note that in node. js You cannot define variables at the outermost layer because all user code belongs to the current module, and the module itself is not the outermost context.
Note: Always use var to define variables to avoid introducing global variables, because global variables pollute namespaces and increase the risk of code coupling.
-filename
filename indicates the file name of the script that is currently executing. It will output the absolute path to the location of the file, and the file name specified with the command-line arguments is not necessarily the same. If the value returned in the module is the path to the module file.
Instance:
Console.log (-filename)
-dirname
DirName represents the directory where the current execution script resides
Instance:
Console.log (-dirname)
SetTimeout (CB,MS)
The SetTimeout (CB,MS) Global function executes the specified function (CB) After the specified number of milliseconds (ms). SetTimeout executes only once for the specified function. Returns a handle value representing the timer.
Instance:
function Printhello () {
Console.log ("Hello,world");
}
Execute the above function after two seconds
SetTimeout (printhello,2000);
Cleartimeout (t)
The Cleartimeout (t) global function is used to stop a timer that was previously created by SetTimeout (). The parameter T is a calculator created by the settimeout () function.
Instance:
function Printhello () {
Console.log ("Hello,world");
}
Execute the above function after two seconds
var t = setTimeout (printhello,2000);
Clear Timer
Cleartimeout (t);
SetInterval (CB,MS)
The SetInterval (CB,MS) Global function executes the specified function (CB) After the specified number of milliseconds (ms). Returns a handle value representing the timer. You can use the cleartimeout (t) function to clear the timer. The SetInterval () method keeps calling functions until Cleartimeout () is called or the window is closed
Instance:
function Printhello () {
Console.log ("Hello,world");
}
Execute the above function after two seconds
SetInterval (printhello,2000);
Console
Console is used to provide console standard output, which is a debugging tool provided by the JScript engine of Internet Explorer, and later becomes a de facto standard for browsers. node. JS uses this standard to provide a console object that is consistent with customary behavior to output characters to the standard output stream (STDOUT) or standard error stream (stderr).
Nodejs (b)