What are the global objects in Node.js? What is the use?
本篇文章带大家了解一下Node.js中的全局对象,看看Node.js中有哪些全局对象,这些全局对象有什么用?怎么用?一起来学习吧!
所谓全局对象,就是可以直接访问的对象,比如
- 浏览器中的的全局对象就是
window
- Node 中的全局对象就是
global
下面让我们看看 Node 的全局对象具体有哪些? 这些对象又有什么用?【推荐学习:《nodejs 教程》】
如何获取全局对象
在某些老项目中,我们可能看到这样的代码片段
const getGlobalThis = () => { // 在 webworker 或 service worker 中 if (typeof self !== "undefined") return self; // 在浏览器中 if (typeof window !== "undefined") return window; // 在 Node.js 中 if (typeof global !== "undefined") return global; // 独立的 JavaScript shell if (typeof this !== "undefined") return this; throw new Error("Unable to locate global object"); }; const theGlobalThis = getGlobalThis(); if (typeof theGlobalThis.setTimeout !== "function") { // 此环境中没有 setTimeout 方法! }
这是因为各个 JS 环境获取全局对象的方式都是不同的,所以通过封装了一个getGlobalThis
来统一获取所有环境的全局对象
但是自从 ES13 特性出现后,支持使用globalThis
这种标准的方式来获取不同环境下的全局 this
对象(也就是全局对象自身)
if (typeof globalThis.setTimeout !== "function") { // 此环境中没有 setTimeout 方法! }
与浏览器相似的全局变量
Node.js 的全局变量与 Chrome 有一些差点,但也有很多类似的
我们启动一个简易的 Node 项目,然后打印如下的东西,看看能否正常输出
Date
对象 ✅
console.log(Date); // [Function: Date]
Math
对象 ✅
console.log(Math); // Object [Math] {}
setTimeout
对象 ✅
console.log(setTimeout); /* [Function: setTimeout] { [Symbol(nodejs.util.promisify.custom)]: [Function] } */
setInterval
对象 ✅
console.log(setInterval); // [Function: setInterval]
setImmediate
对象 ✅
console.log(setImmediate); /* [Function: setImmediate] { [Symbol(nodejs.util.promisify.custom)]: [Function] } */
requestAnimationFrame
对象 ❌
requestAnimationFrame
代表浏览器渲染的下一帧,所以在服务端环境是不存在的
// 浏览器渲染的下一帧,所以在服务端环境中是不存在的 console.log(requestAnimationFrame); // ReferenceError: requestAnimationFrame is not defined
Node.js 特有的全局变量
// 当前代码所运行的脚本位置 console.log(__filename); // D:\MyCode\Gitee\Learning.Node\geek-nodejs\08\index.js // 当前代码所运行的脚本目录 console.log(__dirname); // D:\MyCode\Gitee\Learning.Node\geek-nodejs\08
console.log(process);
version
: Node.js 版本号platform
, arch
: 运行环境的操作系统kill
, exit
: 用于管理,或者杀进程的操作hrtime
: 用于统计时间,可以精确到微秒级cpuUsage
: CPU 占用率resourceUsage
: 内存占用率env
: Node 的环境变量,作用:可以设置环境变量,以便在不同的开发中使用argv
:用户在启动程序时,敲击的命令是怎样的, 作用:命令行程序中使用
........................
更多编程相关知识,请访问:编程入门!!
The above is the detailed content of What are the global objects in Node.js? What is the use?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

How does Node.js do GC (garbage collection)? The following article will take you through it.

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".
