On this page
  1. Proxy & Well-Known Symbols
  2. Contents
  3. Intercepting Operations with Proxy
  4. Well-Known Symbols (JavaScript's Protocol Hooks)

Proxy & Well-Known Symbols

Prev: Promise & Async | Up: builtins | Next: String, RegExp & Intl
Rel: Object & Reflect, Prototype Chain & new Instantiation Flow
Ref: MDN Web Docs — Proxy
Tags: #public #web #javascript #builtins #metaprogramming


Contents


Intercepting Operations with Proxy

Proxy wraps a target object and intercepts fundamental operations (get, set, has, deleteProperty, apply, construct), delegating default behavior via Reflect:

const reactive = (target, onChange) =>
  new Proxy(target, {
    set(obj, prop, value, receiver) {
      const ok = Reflect.set(obj, prop, value, receiver);
      onChange(prop, value);
      return ok;
    },
  });

const state = reactive({ count: 0 }, (k, v) => console.log(`${String(k)} -> ${v}`));
state.count = 1;

Well-Known Symbols (JavaScript's Protocol Hooks)

Well-known symbols play the role of Python's dunder protocol methods:

Linked from

Related notes