Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

what is weakmap and weakset in javascript

/*
	SIMPLE EXPLANATION:

	The WeakMap takes only objects as keys. 
    When one object is not being referred to anymore, it and its
    associated value will get removed from the WeakMap.

    The WeakSet functions similarly.
    Its values are unique and are only objects.
    When one object is not being referred to anymore, it will get removed
    from the WeakSet.
*/
Comment

JavaScript WeakMap

const weakMap = new WeakMap();
console.log(weakMap); // WeakMap {} 

let obj = {};

// adding object (element) to WeakMap
weakMap.set(obj, 'hello');

console.log(weakMap); // WeakMap {{} => "hello"}
Comment

JavaScript WeakMap Methods

const weakMap = new WeakMap();
console.log(weakMap); // WeakMap {} 

let obj = {};

// adding object (element) to WeakMap
weakMap.set(obj, 'hello');

console.log(weakMap); // WeakMap {{} => "hello"}
// get the element of a WeakMap
console.log(weakMap.get(obj)); // hello
// check if an element is present in WeakMap
console.log(weakMap.has(obj)); // true
// delete the element of WeakMap
console.log(weakMap.delete(obj)); // true

console.log(weakMap); // WeakMap {}
Comment

JavaScript WeakMap Methods

const weakMap = new WeakMap();
console.log(weakMap); // WeakMap {} 

let obj = {};

// adding object (element) to WeakMap
weakMap.set(obj, 'hello');

console.log(weakMap); // WeakMap {{} => "hello"}
// get the element of a WeakMap
console.log(weakMap.get(obj)); // hello
// check if an element is present in WeakMap
console.log(weakMap.has(obj)); // true
// delete the element of WeakMap
console.log(weakMap.delete(obj)); // true

console.log(weakMap); // WeakMap {}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript WeakSets Are Not iterable 
Javascript :: javascript remaining elements of an array to a variable using the spread syntax 
Javascript :: javascript for...of with Generators 
Javascript :: JavaScript pauses the async function until the promise 
Javascript :: javascript Deleting an object is not allowed 
Javascript :: electron InitializeSandbox() called with multiple threads in process gpu-process. 
Javascript :: JavaScript Object Prototypes 
Javascript :: react linkify 
Javascript :: The first article title 
Javascript :: what does this operation tell if(!arr.some(isNaN)) JavaScript 
Javascript :: cast uint to address in solidity 
Javascript :: OpenTok Create Session 
Javascript :: npm function-memoizer 
Javascript :: state dependent on prev state in react js 
Javascript :: phaser random circle 
Javascript :: phaser mixed animation 
Javascript :: lookbehind alternative regex 
Javascript :: remove text and keep div inside a div jquery 2 
Javascript :: polygon intersection js 
Javascript :: site:stackoverflow.com two api calls dependent on each other js 
Javascript :: Use Prototype To Add A Property To Javascript Class 
Javascript :: what is computed property in vue js 
Javascript :: forms in angular 
Javascript :: react native ios firebase push notifications not working 
Javascript :: js function arguments 
Javascript :: constructer 
Javascript :: return the first element in an array javascript 
Javascript :: swagger ui express 
Javascript :: convert string to a number javascript 
Javascript :: regular expression remove spaces 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =