O método set() adiciona um novo elemento com a chave e o valor especificados em um objeto WeakMap.

Sintaxe

wm.set(chave, valor);

Parâmetros

chave
Obrigatório. A chave do elemento a ser adicionado no objeto WeakMap.
valor
Obrigatório. O valor do elemento a ser adicionado no objeto WeakMap.

Valor de retorno

O objeto WeakMap.

Exemplos

Utilizando o método set 

var wm = new WeakMap();
var obj = {};

// Adicionar novos elementos ao WeakMap
wm.set(obj, 'foo').set(window, 'bar'); // encadeável

// Atualizar um elemento no WeakMap
wm.set(obj, 'baz');

Especificações

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'WeakMap.prototype.set' in that specification.
Padrão Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'WeakMap.prototype.set' in that specification.
Rascunho  

Compatibilidade de navegadores

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Basic supportChrome Full support 36Edge Full support YesFirefox Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
IE Partial support 11
Notes
Partial support 11
Notes
Notes Returns 'undefined' instead of the 'Map' object.
Opera Full support 23Safari Full support 8WebView Android Full support 37Chrome Android Full support 36Edge Mobile Full support YesFirefox Android Full support 6
Notes
Full support 6
Notes
Notes Prior to Firefox 38, this method threw a TypeError when the key parameter was not an object. This has been fixed in version 38 and later to return false as per the ES2015 standard.
Opera Android Full support 23Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12
Full support 0.12
Full support 0.10
Disabled
Disabled From version 0.10: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
Partial support  
Partial support
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

Notas específicas ao Firefox

  • Antes do Firefox 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30), WeakMap.prototype.set retornava undefined e, portanto, não era encadeável. Isso foi resolvido (bug 1031632). O comportamento também podia ser encontrado no Chrome/v8 (issue).

Ver também

Etiquetas do documento e colaboradores

Colaboradores desta página: mdnwebdocs-bot, jotaRenan
Última atualização por: mdnwebdocs-bot,