Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Symbol
  6. dispose

Symbol.dispose

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

TheSymbol.dispose static data property represents thewell-known symbolSymbol.dispose. Theusing 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 symbolSymbol.dispose.

Property attributes ofSymbol.dispose
Writableno
Enumerableno
Configurableno

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 byawait using. To declare async disposables, useSymbol.asyncDispose.

Examples

User defined disposables

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

js
class Disposable {  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

Specification
ECMAScript Async Explicit Resource Management
# table-1

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp