Nodejs global variables and global objects knowledge points and usage details

Nodejs global variables and global objects knowledge points and usage details

1. Global Object

All modules can be called

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

2) process: points to Node's built-in process module, allowing developers to interact with the current process.

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

3) console: refers to the built-in console module of Node, which provides standard input and standard output functions in the command line environment.

Usually write console.log(), no need to say more

2. Global functions

1) Timer functions: There are 4 timer functions: setTimeout(), clearTimeout(), setInterval(), clearInterval().
2) require: used to load modules.

3. Global variables

1) _filename: points to the name of the script currently running.

2) _dirname: points to the directory where the currently running script is located.

4. Quasi-global variables

The local variables inside the module point to different objects depending on the module, but they are applicable to all modules and can be regarded as pseudo-global variables, mainly module, module.exports, exports, etc.

The module variable refers to the current module. The module.exports variable represents the interface exported by the current module. When other files load the module, they actually read the module.exports variable.

  • module.id The module identifier, usually the module's file name.
  • module.filename The filename of the module.
  • module.loaded returns a boolean value indicating whether the module has finished loading.
  • module.parent returns the module that uses this module.
  • module.children returns an array of other modules that this module uses.

It is important to point out here that the exports variable is actually a link to the module.exports object, which is equivalent to having a line of commands like this in the header of each module.

var exports = module.exports;

The result is that when exporting a module interface, you can add methods to the exports object, but you cannot directly point the exports variable to a function:

exports.custommodule = function (x){ console.log(x);};

The above is invalid because it severs the link between exports and module.exports. However, it is possible to write the following.

Knowledge point expansion:

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

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

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

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

1> Variables defined at the outermost level.

Generally speaking, user code is not at the outermost level.

There is only one case where this is possible: in an interpreter shell environment.

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

var global.x;

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

This is why implicit definitions are not recommended. Such variables defined as global variables will pollute the environment.

This is the end of this article about nodejs global variables and global objects knowledge points and detailed usage. For more related nodejs global variables and global objects content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of nodeJs installation and npm global environment variable configuration
  • Example analysis of global variables in nodejs

<<:  Time zone issues with Django deployed in Docker container

>>:  A brief discussion on order reconstruction: MySQL sharding

Recommend

Detailed steps for setting up and configuring nis domain services on Centos8

Table of contents Introduction to NIS Network env...

Configure Mysql master-slave service implementation example

Configure Mysql master-slave service implementati...

Detailed explanation of JavaScript prototype chain

Table of contents 1. Constructors and instances 2...

JS interview question: Can forEach jump out of the loop?

When I was asked this question, I was ignorant an...

Detailed explanation of the use of title tags and paragraph tags in XHTML

XHTML Headings Overview When we write Word docume...

Details of watch monitoring properties in Vue

Table of contents 1.watch monitors changes in gen...

How to insert weather forecast into your website

We hope to insert the weather forecast into the w...

How to open port 8080 on Alibaba Cloud ECS server

For security reasons, Alibaba Cloud Server ECS co...

Zabbix implements monitoring of multiple mysql processes

Three MySQL instance processes are started on one...

Steps to install GRUB on Linux server

How to Install GRUB for Linux Server You cannot u...

Linux Cron scheduled execution of PHP code with parameters

1. Still use PHP script to execute. Command line ...

React's reconciliation algorithm Diffing algorithm strategy detailed explanation

Table of contents Algorithmic Strategy Single-nod...

Detailed explanation of the calculation method of flex-grow and flex-shrink in flex layout

Flex(彈性布局) in CSS can flexibly control the layout...