Node.js v18.20.8 文档


全局对象#>

【Global objects】

这些对象在所有模块中都可用。以下变量看起来可能是全局的,但实际上不是。它们只存在于模块的作用域中,见 模块系统文档

【These objects are available in all modules. The following variables may appear to be global but are not. They exist only in the scope of modules, see the module system documentation:】

此处列出的对象是 Node.js 特有的。还有一些是 JavaScript 语言本身的一部分,它们也是全局可访问的。

【The objects listed here are specific to Node.js. There are built-in objects that are part of the JavaScript language itself, which are also globally accessible.】

类:AbortController#>

【Class: AbortController

一个用于在选定的基于 Promise 的 API 中发送取消信号的工具类。该 API 基于 Web API AbortController

【A utility class used to signal cancelation in selected Promise-based APIs. The API is based on the Web API AbortController.】

const ac = new AbortController();

ac.signal.addEventListener('abort', () => console.log('Aborted!'),
                           { once: true });

ac.abort();

console.log(ac.signal.aborted);  // Prints true 

abortController.abort([reason])#>

  • reason <any> 一个可选的原因,可以通过 AbortSignalreason 属性获取。

触发中止信号,使 abortController.signal 发出 'abort' 事件。

【Triggers the abort signal, causing the abortController.signal to emit the 'abort' event.】

abortController.signal#>

类:AbortSignal#>

【Class: AbortSignal

AbortSignal 用于在调用 abortController.abort() 方法时通知观察者。

【The AbortSignal is used to notify observers when the abortController.abort() method is called.】

静态方法:AbortSignal.abort([reason])#>

【Static method: AbortSignal.abort([reason])

返回一个已经被中止的新的 AbortSignal

【Returns a new already aborted AbortSignal.】

静态方法:AbortSignal.timeout(delay)#>

【Static method: AbortSignal.timeout(delay)

  • delay <number> 在触发 AbortSignal 之前等待的毫秒数。

返回一个新的 AbortSignal,它将在 delay 毫秒后被中止。

【Returns a new AbortSignal which will be aborted in delay milliseconds.】

静态方法:AbortSignal.any(signals)#>

【Static method: AbortSignal.any(signals)

  • signals <AbortSignal[]> 用于组合成新的 AbortSignalAbortSignal 列表。

返回一个新的 AbortSignal,如果提供的任意信号被中止,该信号也会被中止。它的 abortSignal.reason 将被设置为导致它被中止的 signals 中的任意一个。

【Returns a new AbortSignal which will be aborted if any of the provided signals are aborted. Its abortSignal.reason will be set to whichever one of the signals caused it to be aborted.】

事件:'abort'#>

【Event: 'abort'

当调用 abortController.abort() 方法时,会触发 'abort' 事件。回调函数会使用一个包含单个 type 属性且值为 'abort' 的对象作为参数被调用:

【The 'abort' event is emitted when the abortController.abort() method is called. The callback is invoked with a single object argument with a single type property set to 'abort':】

const ac = new AbortController();

// Use either the onabort property...
ac.signal.onabort = () => console.log('aborted!');

// Or the EventTarget API...
ac.signal.addEventListener('abort', (event) => {
  console.log(event.type);  // Prints 'abort'
}, { once: true });

ac.abort(); 

AbortSignal 所关联的 AbortController 只会触发一次 'abort' 事件。我们建议在添加 'abort' 事件监听器之前,代码先检查 abortSignal.aborted 属性是否为 false

【The AbortController with which the AbortSignal is associated will only ever trigger the 'abort' event once. We recommended that code check that the abortSignal.aborted attribute is false before adding an 'abort' event listener.】

附加到 AbortSignal 的任何事件监听器都应使用 { once: true } 选项(或者如果使用 EventEmitter API 附加监听器,则使用 once() 方法),以确保事件监听器在处理 'abort' 事件后立即被移除。否则可能会导致内存泄漏。

【Any event listeners attached to the AbortSignal should use the { once: true } option (or, if using the EventEmitter APIs to attach a listener, use the once() method) to ensure that the event listener is removed as soon as the 'abort' event is handled. Failure to do so may result in memory leaks.】

abortSignal.aborted#>
  • 类型:<boolean>AbortController 被中止后为 true。
abortSignal.onabort#>

一个可选的回调函数,用户代码可以设置它以在调用 abortController.abort() 函数时收到通知。

【An optional callback function that may be set by user code to be notified when the abortController.abort() function has been called.】

abortSignal.reason#>

AbortSignal 被触发时指定的可选原因。

【An optional reason specified when the AbortSignal was triggered.】

const ac = new AbortController();
ac.abort(new Error('boom!'));
console.log(ac.signal.reason);  // Error: boom! 
abortSignal.throwIfAborted()#>

如果 abortSignal.abortedtrue,则抛出 abortSignal.reason

【If abortSignal.aborted is true, throws abortSignal.reason.】

类:Blob#>

【Class: Blob

参见 <Blob>

【See <Blob>.】

类:Buffer#>

【Class: Buffer

用于处理二进制数据。请参见 缓冲区段

【Used to handle binary data. See the buffer section.】

类:ByteLengthQueuingStrategy#>

【Class: ByteLengthQueuingStrategy

稳定性: 1 - 实验性。

ByteLengthQueuingStrategy 的浏览器兼容实现。

【A browser-compatible implementation of ByteLengthQueuingStrategy.】

__dirname#>

这个变量看起来可能是全局的,但实际上不是。参见 __dirname

【This variable may appear to be global but is not. See __dirname.】

__filename#>

这个变量看起来可能是全局的,但实际上不是。参见 __filename

【This variable may appear to be global but is not. See __filename.】

atob(data)#>

稳定性: 3 - 旧版。请改用 Buffer.from(data, 'base64')

buffer.atob() 的全球别名。

【Global alias for buffer.atob().】

BroadcastChannel#>

参见 <BroadcastChannel>

【See <BroadcastChannel>.】

btoa(data)#>

稳定性: 3 - 过时。请改用 buf.toString('base64')

buffer.btoa() 的全球别名。

【Global alias for buffer.btoa().】

clearImmediate(immediateObject)#>

clearImmediate定时器 部分中有所描述。

clearInterval(intervalObject)#>

clearInterval定时器 部分中有所描述。

clearTimeout(timeoutObject)#>

clearTimeout定时器 部分中有描述。

类:CompressionStream#>

【Class: CompressionStream

稳定性: 1 - 实验性。

CompressionStream 的浏览器兼容实现。

【A browser-compatible implementation of CompressionStream.】

console#>

用于打印到标准输出和标准错误。请参见 console 部分。

【Used to print to stdout and stderr. See the console section.】

类:CountQueuingStrategy#>

【Class: CountQueuingStrategy

稳定性: 1 - 实验性。

CountQueuingStrategy 的浏览器兼容实现。

【A browser-compatible implementation of CountQueuingStrategy.】

Crypto#>

稳定性: 1 - 实验性。使用 --experimental-global-webcrypto CLI 标志启用此 API。

一个与浏览器兼容的 <Crypto> 实现。仅当 Node.js 二进制文件在编译时包含对 node:crypto 模块的支持时,才可使用此全局对象。

【A browser-compatible implementation of <Crypto>. This global is available only if the Node.js binary was compiled with including support for the node:crypto module.】

crypto#>

稳定性: 1 - 实验性。使用 --experimental-global-webcrypto CLI 标志启用此 API。

网络加密 API 的浏览器兼容实现。

【A browser-compatible implementation of the Web Crypto API.】

CryptoKey#>

稳定性: 1 - 实验性。使用 --experimental-global-webcrypto CLI 标志启用此 API。

兼容浏览器的 <CryptoKey> 实现。仅当 Node.js 二进制文件在编译时包含对 node:crypto 模块的支持时,此全局变量才可用。

【A browser-compatible implementation of <CryptoKey>. This global is available only if the Node.js binary was compiled with including support for the node:crypto module.】

CustomEvent#>

稳定性: 1 - 实验性。使用 --experimental-global-customevent CLI 标志启用此 API。

CustomEvent Web API 的浏览器兼容实现。

【A browser-compatible implementation of the CustomEvent Web API.】

类:DecompressionStream#>

【Class: DecompressionStream

稳定性: 1 - 实验性。

DecompressionStream 的浏览器兼容实现。

【A browser-compatible implementation of DecompressionStream.】

Event#>

一个与浏览器兼容的 Event 类实现。更多详情请参见 EventTargetEvent API

【A browser-compatible implementation of the Event class. See EventTarget and Event API for more details.】

EventTarget#>

EventTarget 类的浏览器兼容实现。更多详情请参见 EventTargetEvent API

【A browser-compatible implementation of the EventTarget class. See EventTarget and Event API for more details.】

exports#>

这个变量看起来可能是全局的,但实际上不是。参见 exports

【This variable may appear to be global but is not. See exports.】

fetch#>

稳定性: 1 - 实验性。使用 --no-experimental-fetch CLI 标志禁用此 API。

一个与浏览器兼容的 fetch() 函数实现。

【A browser-compatible implementation of the fetch() function.】

FormData#>

【Class FormData

稳定性: 1 - 实验性。使用 --no-experimental-fetch CLI 标志禁用此 API。

一个与浏览器兼容的 <FormData> 实现。

【A browser-compatible implementation of <FormData>.】

global#>

稳定性: 3 - 传统。请改用 globalThis

在浏览器中,顶层作用域传统上是全局作用域。这意味着 var something 会定义一个新的全局变量,除非在 ECMAScript 模块中。在 Node.js 中情况有所不同。顶层作用域不是全局作用域;Node.js 模块内部的 var something 将是该模块的局部变量,无论它是 CommonJS 模块 还是 ECMAScript 模块

【In browsers, the top-level scope has traditionally been the global scope. This means that var something will define a new global variable, except within ECMAScript modules. In Node.js, this is different. The top-level scope is not the global scope; var something inside a Node.js module will be local to that module, regardless of whether it is a CommonJS module or an ECMAScript module.】

Headers#>

【Class Headers

稳定性: 1 - 实验性。使用 --no-experimental-fetch CLI 标志禁用此 API。

一个与浏览器兼容的 <Headers> 实现。

【A browser-compatible implementation of <Headers>.】

MessageChannel#>

MessageChannel 类。更多详情请参见 MessageChannel

【The MessageChannel class. See MessageChannel for more details.】

MessageEvent#>

MessageEvent 类。更多详情请参见 MessageEvent

【The MessageEvent class. See MessageEvent for more details.】

MessagePort#>

MessagePort 类。更多详情请参见 MessagePort

【The MessagePort class. See MessagePort for more details.】

module#>

这个变量看起来可能是全局的,但实际上不是。参见 module

【This variable may appear to be global but is not. See module.】

performance#>

perf_hooks.performance 对象。

【The perf_hooks.performance object.】

process#>

process 对象。请参阅 process 对象 部分。

【The process object. See the process object section.】

queueMicrotask(callback)#>

queueMicrotask() 方法将微任务排队以调用 callback。如果 callback 抛出异常,将触发 process 对象 'uncaughtException' 事件。

【The queueMicrotask() method queues a microtask to invoke callback. If callback throws an exception, the process object 'uncaughtException' event will be emitted.】

微任务队列由 V8 管理,并且可以以类似于由 Node.js 管理的 process.nextTick() 队列的方式使用。在 Node.js 事件循环的每一轮中,process.nextTick() 队列总是在微任务队列之前处理。

【The microtask queue is managed by V8 and may be used in a similar manner to the process.nextTick() queue, which is managed by Node.js. The process.nextTick() queue is always processed before the microtask queue within each turn of the Node.js event loop.】

// Here, `queueMicrotask()` is used to ensure the 'load' event is always
// emitted asynchronously, and therefore consistently. Using
// `process.nextTick()` here would result in the 'load' event always emitting
// before any other promise jobs.

DataHandler.prototype.load = async function load(key) {
  const hit = this._cache.get(key);
  if (hit !== undefined) {
    queueMicrotask(() => {
      this.emit('load', hit);
    });
    return;
  }

  const data = await fetchData(key);
  this._cache.set(key, data);
  this.emit('load', data);
}; 

类:ReadableByteStreamController#>

【Class: ReadableByteStreamController

稳定性: 1 - 实验性。

ReadableByteStreamController 的浏览器兼容实现。

【A browser-compatible implementation of ReadableByteStreamController.】

类:ReadableStream#>

【Class: ReadableStream

稳定性: 1 - 实验性。

ReadableStream 的浏览器兼容实现。

【A browser-compatible implementation of ReadableStream.】

类:ReadableStreamBYOBReader#>

【Class: ReadableStreamBYOBReader

稳定性: 1 - 实验性。

ReadableStreamBYOBReader 的浏览器兼容实现。

【A browser-compatible implementation of ReadableStreamBYOBReader.】

类:ReadableStreamBYOBRequest#>

【Class: ReadableStreamBYOBRequest

稳定性: 1 - 实验性。

ReadableStreamBYOBRequest 的浏览器兼容实现。

【A browser-compatible implementation of ReadableStreamBYOBRequest.】

类:ReadableStreamDefaultController#>

【Class: ReadableStreamDefaultController

稳定性: 1 - 实验性。

ReadableStreamDefaultController 的浏览器兼容实现。

【A browser-compatible implementation of ReadableStreamDefaultController.】

类:ReadableStreamDefaultReader#>

【Class: ReadableStreamDefaultReader

稳定性: 1 - 实验性。

ReadableStreamDefaultReader 的浏览器兼容实现。

【A browser-compatible implementation of ReadableStreamDefaultReader.】

require()#>

这个变量看起来可能是全局的,但实际上不是。参见 require()

【This variable may appear to be global but is not. See require().】

Response#>

稳定性: 1 - 实验性。使用 --no-experimental-fetch CLI 标志禁用此 API。

一个与浏览器兼容的 <Response> 实现。

【A browser-compatible implementation of <Response>.】

Request#>

稳定性: 1 - 实验性。使用 --no-experimental-fetch CLI 标志禁用此 API。

一个与浏览器兼容的 <Request> 实现。

【A browser-compatible implementation of <Request>.】

setImmediate(callback[, ...args])#>

setImmediate定时器 部分中有所描述。

setInterval(callback, delay[, ...args])#>

setInterval定时器 部分中有所描述。

setTimeout(callback, delay[, ...args])#>

setTimeout定时器 部分中有所描述。

structuredClone(value[, options])#>

WHATWG structuredClone 方法。

【The WHATWG structuredClone method.】

SubtleCrypto#>

稳定性: 1 - 实验性。使用 --experimental-global-webcrypto CLI 标志启用此 API。

一个与浏览器兼容的 <SubtleCrypto> 实现。仅当 Node.js 二进制文件在编译时包含对 node:crypto 模块的支持时,才可使用此全局对象。

【A browser-compatible implementation of <SubtleCrypto>. This global is available only if the Node.js binary was compiled with including support for the node:crypto module.】

DOMException#>

WHATWG 的 DOMException 类。更多详情请参见 DOMException

【The WHATWG DOMException class. See DOMException for more details.】

TextDecoder#>

WHATWG TextDecoder 类。请参见 TextDecoder 节。

【The WHATWG TextDecoder class. See the TextDecoder section.】

类:TextDecoderStream#>

【Class: TextDecoderStream

稳定性: 1 - 实验性。

TextDecoderStream 的浏览器兼容实现。

【A browser-compatible implementation of TextDecoderStream.】

TextEncoder#>

WHATWG TextEncoder 类。请参见 TextEncoder 节。

【The WHATWG TextEncoder class. See the TextEncoder section.】

类:TextEncoderStream#>

【Class: TextEncoderStream

稳定性: 1 - 实验性。

TextEncoderStream 的浏览器兼容实现。

【A browser-compatible implementation of TextEncoderStream.】

类:TransformStream#>

【Class: TransformStream

稳定性: 1 - 实验性。

TransformStream 的浏览器兼容实现。

【A browser-compatible implementation of TransformStream.】

类:TransformStreamDefaultController#>

【Class: TransformStreamDefaultController

稳定性: 1 - 实验性。

TransformStreamDefaultController 的浏览器兼容实现。

【A browser-compatible implementation of TransformStreamDefaultController.】

URL#>

WHATWG 的 URL 类。参见 URL 节。

【The WHATWG URL class. See the URL section.】

URLSearchParams#>

WHATWG 的 URLSearchParams 类。参见 URLSearchParams 部分。

【The WHATWG URLSearchParams class. See the URLSearchParams section.】

WebAssembly#>

作为所有 W3C WebAssembly 相关功能命名空间的对象。有关用法和兼容性,请参见 Mozilla 开发者网络

【The object that acts as the namespace for all W3C WebAssembly related functionality. See the Mozilla Developer Network for usage and compatibility.】

类:WritableStream#>

【Class: WritableStream

稳定性: 1 - 实验性。

WritableStream 的浏览器兼容实现。

【A browser-compatible implementation of WritableStream.】

类:WritableStreamDefaultController#>

【Class: WritableStreamDefaultController

稳定性: 1 - 实验性。

WritableStreamDefaultController 的浏览器兼容实现。

【A browser-compatible implementation of WritableStreamDefaultController.】

类:WritableStreamDefaultWriter#>

【Class: WritableStreamDefaultWriter

稳定性: 1 - 实验性。

WritableStreamDefaultWriter 的浏览器兼容实现。

【A browser-compatible implementation of WritableStreamDefaultWriter.】

Node.js 中文网 - 粤ICP备13048890号