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

How to implement Vue timer

This article example shares the specific code of ...

Detailed steps for installing and using vmware esxi6.5

Table of contents Introduction Architecture Advan...

JavaScript determines whether the browser is IE

As a front-end developer, I can’t avoid IE’s pitf...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

SQL serial number acquisition code example

This article mainly introduces the sql serial num...

Detailed explanation of mysql replication tool based on python

Table of contents 1. Introduction Second practice...

Sample code for implementing markdown automatic numbering with pure CSS

The origin of the problem The first time I paid a...

Detailed tutorial on installing and using Kong API Gateway with Docker

1 Introduction Kong is not a simple product. The ...

Style trigger effect of web page input box

<br />This example mainly studies two parame...

How to install PHP7 Redis extension on CentOS7

Introduction In the previous article, we installe...

MySQL intercepts the sql statement of the string function

1. left(name,4) intercepts the 4 characters on th...

Detailed explanation of daily_routine example code in Linux

First look at the example code: #/bin/bash cal da...

JavaScript canvas realizes dynamic point and line effect

This article shares the specific code for JavaScr...