TGViewer
JavaScript JavaScript @javascript · 31K subscribers
Post #2289 4.86K
CHALLENGE

const target = { a: 1, b: 2 };
const handler = {
get(obj, prop) {
return prop === 'sum' ? obj.a + obj.b : Reflect.get(obj, prop);
},
set(obj, prop, value) {
if (prop === 'a' && value < 0) {
return false;
}
return Reflect.set(obj, prop, value);
}
};

const proxy = new Proxy(target, handler);
proxy.a = -5;
proxy.b = 10;
console.log(`${proxy.a}, ${proxy.b}, ${proxy.sum}`);
  • 👍 4
  • 🔥 1
More from @javascript
  1. Oct 6, 2026Post #3542
  2. Oct 6, 2026CHALLENGE async function* gen() { const x = yield 1; const y = yield x + 1; return x + y;…
  3. Oct 5, 2026🎵 Tonny Espeset has recreated the '80s hit Take on Me by A-ha in pure JavaScript (along w…
  4. Oct 5, 2026Post #3539
  5. Oct 5, 2026CHALLENGE class Base { static log = []; static { Base.log.push('base-block'); } static val…
  6. Oct 4, 2026👀 Size Limit: A JavaScript Performance Budgeting Tool Set a size or load-time budget for…
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 →