What are the practical applications of ES6 WeakMap

0 votes
Can you tell me What are the practical applications of ES6 WeakMap?
Feb 10 in Node-js by Ashutosh
• 17,360 points
44 views

1 answer to this question.

0 votes

Practical Applications of WeakMap:

Private Data Storage:

Store private data for objects that can't be accessed directly, ensuring data security.

const privateData = new WeakMap();

class Person {

  constructor(name) {

    privateData.set(this, { name });

  }

  getName() {

    return privateData.get(this).name;

  }

}

const person = new Person('Alice');

console.log(person.getName()); // Alice

Metadata Association:

Attach extra data (metadata) to objects without modifying them directly.

const metadata = new WeakMap();

const obj = {};

metadata.set(obj, { id: 1, info: "example" });

console.log(metadata.get(obj)); // { id: 1, info: "example" }

Event Listener Management:

Track event listeners to avoid memory leaks when elements are removed.

const listeners = new WeakMap();

const button = document.querySelector('button');

function clickHandler() {

  console.log('Clicked!');

}

listeners.set(button, clickHandler);

button.addEventListener('click', clickHandler);

Caching Results:

Cache computed results to save processing time.

const cache = new WeakMap();

function compute(obj) {

  if (cache.has(obj)) return cache.get(obj);

  const result = obj.value * 2; // Example calculation

  cache.set(obj, result);

  return result;

}

answered Feb 10 by Navya

Related Questions In Node-js

0 votes
1 answer

What are the limitations of React Native?

React Native is a popular framework for ...READ MORE

answered Dec 12, 2024 in Node-js by Navya
100 views
0 votes
0 answers
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,842 views
0 votes
1 answer

What are the approaches to testing in React?

Testing in React ensures your components, logic, ...READ MORE

answered Dec 12, 2024 in Node-js by Navya
83 views
0 votes
1 answer

How do I transform an array into an object?

Here are some common approaches: Using Array.prototype.reduce(): Example: const array ...READ MORE

answered Feb 10 in Node-js by Navya
42 views
0 votes
1 answer

How do I set Resharper's language level for ECMAScript 6?

To configure ReSharper to recognize ECMAScript 6 ...READ MORE

answered Feb 10 in Node-js by Navya
43 views
0 votes
1 answer

What is the correct method to clone a JavaScript object?

Cloning a JavaScript object can be achieved ...READ MORE

answered Feb 10 in Node-js by Navya
53 views
0 votes
1 answer

What is the process to parse JSON using Node.js?

You can parse JSON data using the ...READ MORE

answered Feb 10 in Node-js by Navya
37 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP