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 :: how to get child element in javascript 
Javascript :: about ajax 
Javascript :: Passing a state as a prop in react 
Javascript :: how to parse header in node.js lambda 
Javascript :: Stringy.JS 
Javascript :: max value in an array 
Javascript :: change icon on click angular 
Javascript :: LRANGE in redis 
Javascript :: mongoose populate array of ids 
Javascript :: append item to array javascript 
Javascript :: javascript if function multiple conditions 
Javascript :: data types in js 
Javascript :: react s3 
Javascript :: how to debug node js file in webpack 
Javascript :: require vs import 
Javascript :: async.each javascript 
Javascript :: time difference in javascript 
Javascript :: file upload in node js 
Javascript :: map values js 
Javascript :: value js 
Javascript :: take one character in end of string javascript 
Javascript :: how to stop angular server 
Javascript :: how to remove an object from javascript array 
Javascript :: json date format 
Javascript :: how to use ejs with client side ejs 
Javascript :: .then(async 
Javascript :: createtextnode javascript 
Javascript :: get syntethicbaseevent and parameter in react 
Javascript :: Javascript async await & Promise 
Javascript :: scrollintoview 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =