WeakSet

by 2 contributors:

This article is in need of an editorial review.

This translation is incomplete. Please help translate this article from English.

これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。

概要

コレクションとして弱保持されたobjectsを格納します。

構文

 new WeakSet([iterable]);

引数

iterable
iterable object が渡された場合、すべての要素は、新しいWeakSetに追加されます。nullはundefinedとして扱われます。

説明

WeakSetオブジェクトはコレクションオブジェクトです。WeakSet内のオブジェクトは一度だけ発生します。すなわち、WeakSetコレクション内で唯一の要素になります。

Setオブジェクトとの主な違いは下記の通りです。:

  • ISetsとの対比で、WeakSetsオブジェクトのみのコレクションです。すべての型のの任意の値のコレクションではありません。
  • WeakSetweakです。: コレクション内のオブジェクトへの参照は弱保持です。WeakSet内に格納されているオブジェクトへの参照が他にない場合、ガベージコレクションされます。これは、コレクション内に格納されている現在のオブジェクトのリストがないことを意味します。WeakSetsは列挙されません。

プロパティ

WeakSet.length
lengthプロパティの値は0です。
WeakSet.prototype
Setコンストラクタに対するプロトタイプを表します。すべてのWeakSetオブジェクトにプロパティを追加できます。

WeakSet インスタンス

すべてのWeakSet インスタンスはWeakSet.prototypeから継承します。

プロパティ

WeakSet.prototype.constructor
Returns the function that created an instance's prototype. This is the WeakSet function by default.

Methods

WeakSet.prototype.add(value)
Appends a new element with the given value to the WeakSet object.
WeakSet.prototype.delete(value)
Removes the element associated to the value. WeakSet.prototype.has(value) will return false afterwards.
WeakSet.prototype.has(value)
Returns a boolean asserting whether an element is present with the given value in the WeakSet object or not.
WeakSet.prototype.clear()
Removes all elements from the WeakSet object.

例: WeakSet オブジェクトを使う

var ws = new WeakSet();
var obj = {};
var foo = {};

ws.add(window);
ws.add(obj);

ws.has(window); // true
ws.has(foo);    // false, foo has not been added to the set

ws.delete(window); // removes window from the set
ws.has(window);    // false, window has been removed

ws.clear(); // empty the whole WeakSet

仕様

仕様 ステータス コメント
ECMAScript 6 (ECMA-262)
The definition of 'WeakSet' in that specification.
勧告候補 初期定義。

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート 36 34 (34) 未サポート 23 未サポート
new WeakSet(iterable) 38 34 (34) 未サポート 25 未サポート
コンストラクタ 引数: new WeakSet(null) (有) 37 (37) ? ? ?
コンストラクタ内のMonkeyパッチを適用したadd() (有) 37 (37) ? ? ?
機能 Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本サポート 未サポート 34.0 (34) 未サポート 未サポート 未サポート
new WeakMap(iterable) 未サポート 34.0 (34) 未サポート 未サポート 未サポート
コンストラクタ 引数: new WeakSet(null) ? (有) ? ? ?
コンストラクタ内のMonkeyパッチを適用したadd() ? (有) ? ? ?

関連情報

ドキュメントのタグと貢献者

Contributors to this page: shide55, Jeremie
最終更新者: shide55,