Question 53
Question
Answer
class Person {
constructor(name) {
this.name = name;
this._relationships = new WeakMap(); // Store relationships (e.g., 'friendsWith')
}
addFriend(friend) {
this._relationships.set('friendsWith', friend);
friend._relationships.set('knows', this);
}
}
const john = new Person('John');
const jane = new Person('Jane');
john.addFriend(jane);
// Now, 'john' and 'jane' have a relationship via their _relationships WeakMaps.Last updated