TGViewer
JavaScript JavaScript @javascript · 31K subscribers
Post #2244 4.95K
CHALLENGE

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);
  • 👍 4
  • ❤ 1
More from @javascript
  1. Oct 9, 2026Post #3548
  2. Oct 9, 2026CHALLENGE const a = -7 % 3; const b = 7 % -3; const c = 2 ** 3 ** 2; const d = (2 ** 3) **…
  3. Oct 8, 2026Post #3546
  4. Oct 8, 2026CHALLENGE function tag(strings, ...values) { return strings.raw.join('|') + '::' + values.…
  5. Oct 7, 2026Post #3544
  6. Oct 7, 2026CHALLENGE const sym1 = Symbol('id'); const sym2 = Symbol('id'); const obj = { [sym1]: 'fir…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →