大网数据广告活动
Web 标准与前端开发

Symbol.dispose

· 52CE 我爱测

The **`Symbol.dispose`** static data property represents the well-known symbol `Symbol.dispose`. The Statements/using declaration looks up this symbol on the variable initializer for the method to call when the variable goes out of scope.

The Symbol.dispose static data property represents the well-known symbol Symbol.dispose. The Statements/using declaration looks up this symbol on the variable initializer for the method to call when the variable goes out of scope.

Value

The well-known symbol Symbol.dispose.

Description

An object is disposable if it has the [Symbol.dispose]() method. The method is expected to have the following semantics:

  • Invoking this method notifies the Disposable object that the caller does not intend to continue to use this object. This method should perform any necessary logic to explicit clean up the resource including, but not limited to, file system handles, streams, host objects, etc.
  • When an exception is thrown from this method, it typically means that the resource could not be explicitly freed.
  • If called more than once on the same object, the function should not throw an exception. However, this requirement is not enforced.

This method should not return a promise, as promises returned by [Symbol.dispose]() are not awaited by Statements/await_using. To declare async disposables, use Symbol.asyncDispose.

Examples

User defined disposables

[Symbol.dispose] allows the creation of custom disposables. See the Statements/using reference for more information.

class Disposable {
  #disposed;
  constructor() {
    this.#disposed = false;
  }

  [Symbol.dispose]() {
    this.#disposed = true;
  }

  get isDisposed() {
    return this.#disposed;
  }
}

const resource = new Disposable();
{
  using resourceUsed = resource;
  console.log(resource.isDisposed); // false
}
console.log(resource.isDisposed); // true

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。