Home  >  Article  >  Web Front-end  >  How to define global variables in nodejs

How to define global variables in nodejs

青灯夜游
青灯夜游Original
2021-10-29 16:47:368028browse

In nodejs, you can use the global object global to define global variables. All global variables are attributes of the global object. The definition syntax is "global. variable name="variable value";"; you can also use "global .variable name" statement to access the global variable.

How to define global variables in nodejs

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, Dell G3 computer.

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.

Method to define global variables:

global.变量名="变量值";

Method to access global variables:

global.变量名

[Recommended learning: "nodejs Tutorial"]

The above is the detailed content of How to define global variables in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn