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 does the node global object mean?

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what the node global object refers to". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "what the node global object refers to" can help you solve the problem.

In node, global objects can be directly used without references, which can be divided into: 1, global objects for module packaging; 2, process objects; 3, console Console modules; 4, EventLoop-related api;5, Buffer objects; 6, global.

Operating environment of this tutorial: windows10 system, nodejs version 12.19.0, Dell G3 computer.

What is the global object of node

A global object is an object that can be used directly without reference. It should be noted that global objects are different from the global keyword.

Nodejs global objects fall into the following categories:

1. Global objects used for module packaging

(1) exports

(2) module

(3) require

(4) _ _ filename: current file name

(5) _ _ dirname: current file directory

2. Process object

3. Console Console module

4. EventLoop related api

(1) setImmediate

(2) setInterval

(3) setTimeout

(4) related clear

5. Buffer object

6 、 global

All global variables (except global itself) are properties of the global object

Examples are as follows: Class:Buffer

Can handle binary and non-Unicode encoded data

The raw data is stored in the Buffer class instantiation. Buffer is similar to an array of integers, which is allocated memory in the original storage space of the V8 heap

Once a Buffer instance is created, it cannot be resized

Process

Process object that provides information and control about the current process

Including in the process of executing the node program, if you need to pass a parameter, we want to get this parameter in the process built-in object

Start the process:

Node index.js parameter 1, parameter 2, parameter 3

The index.js file is as follows:

Process.argv.forEach ((val, index) = > {console.log (`${index}: ${val}`);})

The output is as follows:

/ usr/local/bin/node/Users/mjr/work/node/process-args.js parameter 1, parameter 2, parameter 3

In addition, it also includes some other information such as version, operating system, etc.

Console

Used to print stdout and stderr

The most common way to enter content: console.log

Console.log ("hello")

Clear the console: console.clear

Console.clear

Call stack of print function: console.trace

Function test () {demo ();} function demo () {foo ();} function foo () {console.trace ();} test ()

ClearInterval 、 setInterval

Set timer and clear timer

SetInterval (callback, delay [,... args])

Callback repeats every delay millisecond

ClearInterval is the corresponding method of sending and canceling timers.

ClearTimeout 、 setTimeout

Set delay timer and clear delay timer

SetTimeout (callback,delay [,... args])

Callback executes once after delay millisecond

ClearTimeout is the corresponding method to cancel the delay timer.

Global

Global namespace objects, process, console, setTimeout, etc. mentioned on the wall are all put into global.

Global objects at the console.log (process = global.process) / / true module level

These global objects are variables in the module, but they are available in each module and look like global variables, as if they cannot be used in command interactions, including:

_ _ dirname

_ _ filename

Exports

Module

Require

_ _ dirname

Gets the path where the current file is located, excluding the following file name

Run node example.js from / Users/mjr:

Console.log (_ _ dirname); / / print: / Users/mjr__filename

Get the path and file name of the current file, including the following file name

Run node example.js from / Users/mjr:

Console.log (_ _ filename); / / print: / Users/mjr/example.jsexports

Module.exports is used to specify what is exported by a module, that is, what can be accessed through require ()

Exports.name = name;exports.age = age;exports.sayHello = sayHello;module

A reference to the current module that is used through module.exports to specify what is exported by a module, that is, content that can be accessed through require ()

Require

Used to import modules, JSON, or local files. Modules can be imported from node_modules.

You can use relative paths to import local modules or JSON files, which are processed according to the directory name defined by _ _ dirname or the current working directory

This is the end of the content about "what does the node global object mean?" thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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