Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla
Your Search Results

    WeakSet

    This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
    Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

    Summary

    The WeakSet object lets you store weakly held objects in a collection.

    Syntax

     new WeakSet([iterable]);

    Parameters

    iterable
    If an iterable object is passed, all of its elements will be added to the new WeakSet. null is treated as undefined.

    Description

    WeakSet objects are collections of objects. An object in the WeakSet may only occur once; it is unique in the WeakSet's collection.

    The main differences to the Set object are:

    • In contrast to Sets, WeakSets are collections of objects only and not of arbitrary values of any type.
    • The WeakSet is weak: References to objects in the collection are held weakly. If there is no other reference to an object stored in the WeakSet, they can be garbage collected. That also means that there is no list of current objects stored in the collection. WeakSets are not enumerable.

    Properties

    WeakSet.length
    The value of the length property is 1.
    WeakSet.prototype
    Represents the prototype for the Set constructor. Allows the addition of properties to all WeakSet objects.

    WeakSet instances

    All WeakSet instances inherit from WeakSet.prototype.

    Properties

    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.

    Examples

    Example: Using the WeakSet object

    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
    

    Specifications

    Specification Status Comment
    ECMAScript 6 (ECMA-262)
    The definition of 'WeakSet' in that specification.
    Draft Initial definition.

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support 36 34 (34) Not supported 23 Not supported
    new WeakSet(iterable) 38 34 (34) Not supported 25 Not supported
    Constructor argument: new WeakSet(null) (Yes) 37 (37) ? ? ?
    Monkey-patched add() in Constructor (Yes) 37 (37) ? ? ?
    Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support Not supported 34.0 (34) Not supported Not supported Not supported
    new WeakMap(iterable) Not supported 34.0 (34) Not supported Not supported Not supported
    Constructor argument: new WeakSet(null) ? (Yes) 37.0 (37) ? ? ?
    Monkey-patched add() in Constructor ? (Yes) 37.0 (37) ? ? ?

    See also

    Document Tags and Contributors

    Contributors to this page: fscholz, phistuck, m_gol, arai, claudepache, teoli, realityking, sanderd17
    Last updated by: arai,