const privateData = new WeakMap();
function Person(name) {
privateData.set(this, { name, secretCount: 0 });
this.greet = function() {
const data = privateData.get(this);
data.secretCount++;
return `Hello, my name is ${data.name}`;
};
this.getSecretCount = function() {
return privateData.get(this).secretCount;
};
}
const alice = new Person('Alice');
alice.greet();
alice.greet();
const result = [
privateData.has(alice),
alice.name,
alice.getSecretCount()
];
console.log(result);
Post #2244
4.95K
CHALLENGE
- 👍 4
- ❤ 1