Home >Web Front-end >Front-end Q&A >What is the top-level object of node
The top-level object of node is the global object. The global object is the global object of node. This object and all its properties can be accessed anywhere in the program; the most fundamental role of the global object is to serve as the host of global variables.
The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.
The top-level object refers to the window in the browser and the global object in the node environment.
There is a special object in JavaScript called the global object (Global Object). It and all its properties can be accessed anywhere in the program, that is, global variables.
In browser JavaScript, usually window is the global object, and the global object in Node.js is global. All global variables (except global itself) are properties of the global object.
In Node.js we can directly access global properties without including it in the application.
Global objects and global variables
The most fundamental role of global is to serve as the host of global variables. According to the definition of ECMAScript, variables that meet the following conditions are global variables:
Variables defined in the outermost layer;
Properties of global objects ;
Implicitly defined variables (variables that are not directly assigned a value).
When you define a global variable, the variable also becomes a property of the global object, and vice versa. It should be noted that in Node.js you cannot define variables at the outermost level, because all user code belongs to the current module, and the module itself is not the outermost context.
Note: It is best not to use var to define variables to avoid introducing global variables, because global variables will pollute the namespace and increase the risk of code coupling.
The __filename attribute and __dirname attribute of the global object
__filename
Attribute: Returns the file of the currently executed file Path, the path is the absolute path after parsing. In the module, the path is the path of the module file. This attribute is not a global attribute, but the
__dirname
attribute of the module: Return The path to the directory where the currently executing script file is located. This attribute is also for the module, rather than other common objects under the global
global object. There are many other common objects. Please refer to the API. (http://nodejs.cn/api/globals.html)
For more node-related knowledge, please visit: nodejs tutorial!
The above is the detailed content of What is the top-level object of node. For more information, please follow other related articles on the PHP Chinese website!