Table of Contents
如何获取全局对象
与浏览器相似的全局变量
Node.js 特有的全局变量
Home Web Front-end JS Tutorial What are the global objects in Node.js? What is the use?

What are the global objects in Node.js? What is the use?

Jun 30, 2021 am 11:13 AM
node.js

本篇文章带大家了解一下Node.js中的全局对象,看看Node.js中有哪些全局对象,这些全局对象有什么用?怎么用?一起来学习吧!

What are the global objects in Node.js? What is the use?

所谓全局对象,就是可以直接访问的对象,比如

  • 浏览器中的的全局对象就是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 方法!
}
Copy after login

这是因为各个 JS 环境获取全局对象的方式都是不同的,所以通过封装了一个getGlobalThis来统一获取所有环境的全局对象

但是自从 ES13 特性出现后,支持使用globalThis 这种标准的方式来获取不同环境下的全局 this 对象(也就是全局对象自身)

if (typeof globalThis.setTimeout !== "function") {
  // 此环境中没有 setTimeout 方法!
}
Copy after login

与浏览器相似的全局变量

Node.js 的全局变量与 Chrome 有一些差点,但也有很多类似的

我们启动一个简易的 Node 项目,然后打印如下的东西,看看能否正常输出

  • Date 对象 ✅
console.log(Date);
// [Function: Date]
Copy after login
  • Math 对象 ✅
console.log(Math);
// Object [Math] {}
Copy after login
  • setTimeout 对象 ✅
console.log(setTimeout);
/*
[Function: setTimeout] {
  [Symbol(nodejs.util.promisify.custom)]: [Function]
}
*/
Copy after login
  • setInterval 对象 ✅
console.log(setInterval);
// [Function: setInterval]
Copy after login
  • setImmediate 对象 ✅
console.log(setImmediate);
/*
[Function: setImmediate] {
  [Symbol(nodejs.util.promisify.custom)]: [Function]
}
*/
Copy after login
  • requestAnimationFrame 对象 ❌

requestAnimationFrame代表浏览器渲染的下一帧,所以在服务端环境是不存在的

// 浏览器渲染的下一帧,所以在服务端环境中是不存在的
console.log(requestAnimationFrame);
// ReferenceError: requestAnimationFrame is not defined
Copy after login

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
Copy after login
console.log(process);
Copy after login

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!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

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

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

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!

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

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?

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

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.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

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!

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

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

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

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!

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

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".

See all articles