Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the top-level object in node?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the top-level object in node refers to, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

The top-level object of node is the global object. The global object is the global object of node, which and all its properties can be accessed anywhere in the program; the most fundamental function of the global object is to host global variables.

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

The top-level object, in the browser, refers to the window and in the node environment refers to the global object.

There is a special object in JavaScript called the global object (Global Object), which and all its properties can be accessed anywhere in the program, that is, global variables.

In browser JavaScript, usually window is a global object, while the global object in Node.js is global, and all global variables (except 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 application.

Global objects and global variables

The most fundamental role of global is to host 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 the global object

Implicitly defined variables (variables that are not directly assigned are not defined).

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: it is best not to use var to define variables to avoid introducing global variables, which can contaminate the namespace and increase the risk of code coupling.

The _ _ filename and _ _ dirname properties of the global object

_ _ filename attribute: returns the file path of the currently executed file, which 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 a module's.

_ _ dirname attribute: returns the path to the directory where the script file is currently executed, which is also module, not global

Other commonly used objects under the global object, as well as many other commonly used objects, refer to API (http://nodejs.cn/api/globals.html)

Thank you for reading this article carefully. I hope the article "what do the top-level objects in node refer to" shared by the editor will be helpful to you? at the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report