Question 51
Question
Answer
class MyClass { constructor() { this._data = new WeakMap(); // Create a private WeakMap } setData(key, value) { this._data.set(this, value); // Set data associated with this instance } getData(key) { return this._data.get(this, key); // Retrieve data associated with this instance } } const myInstance = new MyClass(); myInstance.setData('name', 'Alice'); // Set private data console.log(myInstance.getData('name')); // Access private data
Last updated