Skip to content

Latest commit

 

History

History
90 lines (73 loc) · 3.04 KB

File metadata and controls

90 lines (73 loc) · 3.04 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs ms.assetid caps.latest.revision author ms.author manager
WeakMap Object (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
WeakMap
JavaScript
TypeScript
DHTML
4682d2dc-caf9-4fa8-8313-a0a0b804fd1d
9
mikejo5000
mikejo
ghogen

WeakMap Object (JavaScript)

A collection of key/value pairs in which each key is an object reference.

Syntax

weakmapObj = new WeakMap()  

Remarks

A WeakMap object cannot be enumerated.

If you add a value to the collection using an existing key, the new value will replace the old value.

In a WeakMap object, references to key objects are held 'weakly'. This means that WeakMap will not prevent a garbage collection from happening on the key objects. When there are no references (other than WeakMap) to the key objects, the garbage collector may collect the key objects.

Properties

The following table lists the properties of the WeakMap object.

Property Description
constructor Specifies the function that creates a WeakMap.
prototype Returns a reference to the prototype for a WeakMap.

Methods

The following table lists the methods of the WeakMap object.

Method Description
clear Removes all elements from a WeakMap.
delete Removes a specified element from a WeakMap.
get Returns a specified element from a WeakMap.
has Returns true if the WeakMap contains a specified element.
set Adds a new element to a WeakMap.
toString Returns a string representation of a WeakMap.
valueOf Returns the primitive value of the specified object.

Example

The following example shows how to add members to a WeakMap object and then retrieve them.

var dog = {  
    breed: "yorkie"  
}  
  
var cat = {  
    breed: "burmese"  
}  
  
var wm = new WeakMap();  
wm.set(dog, "fido");  
wm.set(cat, "pepper");  
  
document.write(wm.get(dog) + ": ");  
document.write(dog.breed);  
document.write("<br />");  
document.write(wm.get(cat) + ": ");  
document.write(cat.breed);  
  
// Output:  
// fido: yorkie  
// pepper: burmese  
  

Requirements

[!INCLUDEjsv11]