本篇文章带大家了解一下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!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
