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

How to use nodejs global variables and global objects

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use nodejs global variables and global objects". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use nodejs global variables and global objects.

1. Global object

All modules can be called

1) global: represents the global environment in which Node is located, similar to the window object in the browser.

2) process: point to Node's built-in process module, which allows developers to interact with the current process.

For example, if you type node directly in DOS or terminal window, you will enter the command line mode of NODE (REPL environment). To quit, you can enter process.exit ()

3) console: point to the built-in console module of Node to provide standard input and standard output functions in the command line environment.

Usually write console.log (), needless to say

2. Global function

1) timer function: there are 4 timer functions, namely setTimeout (), clearTimeout (), setInterval (), clearInterval ().

2) require: used to load the module.

3. Global variables

1) _ filename: points to the file name of the currently running script.

2) _ dirname: point to the directory where the currently running script is located.

4. Quasi-global variables

The local variables within the module point to different objects according to different modules, but all modules are applicable and can be regarded as pseudo-global variables, mainly module, module.exports, exports and so on.

The module variable refers to the current module. The module.exports variable represents the output interface of the current module, and other files load the module, which is actually reading the module.exports variable.

The identifier of the module.id module, usually the file name of the module.

The file name of the module.filename module.

Module.loaded returns a Boolean value indicating whether the module has finished loading.

Module.parent returns the module that uses the module.

Module.children returns an array of other modules to be used by the module.

In particular, the exports variable is actually a link to the module.exports object, which is equivalent to a line of such a command in the header of each module.

Var exports = module.exports

As a result, you can add methods to the exports object when outputting the module interface, but you cannot directly point the exports variable to a function:

Exports. Custom module = function (x) {console.log (x);}

The above writing is invalid because it cuts off the link between exports and module.exports. However, it is OK to write as follows.

Knowledge point expansion:

There is a special object in JavaScript called the global object.

In browser JS, this global object is usually a Window object

In NodeJS, the name of this global object is global.

In NodeJS, there are three ways to define global variables:

1 > the variable defined in the outermost layer.

Generally speaking, the user code will not be on the outermost layer.

There is only one situation: in the interpreter shell environment.

2 > define the variable as a property of the global object

Var global.x

3 > all variables implicitly defined (undefined, directly assigned variables)

This is why implicit definitions are not recommended, as such variables are defined as global variables that pollute the environment.

At this point, I believe you have a deeper understanding of "how to use nodejs global variables and global objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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