TechTalks
Chapter 9 of 12

9.1 Metaprogramming with Proxy & Reflect

Metaprogramming is writing code that reads, inspects, or modifies the behavior of other code at runtime. The Proxy object wraps a target object and intercepts operations (traps).

Building a Reactive State Store with Proxyjavascript
function createReactiveStore(initialState, onChange) {
  return new Proxy(initialState, {
    get(target, prop, receiver) {
      return Reflect.get(target, prop, receiver);
    },
    set(target, prop, value, receiver) {
      const oldValue = target[prop];
      const success = Reflect.set(target, prop, value, receiver);
      if (success && oldValue !== value) {
        onChange(prop, value);
      }
      return success;
    }
  });
}

const store = createReactiveStore({ count: 0 }, (key, val) => {
  console.log(`UI Trigger: ${key} updated to ${val}`);
});

store.count++; // 'UI Trigger: count updated to 1'

9.2 Functional Programming: Composition & Currying

Currying & Function Compositionjavascript
// Currying
const multiply = a => b => a * b;
const double = multiply(2);
console.log(double(5)); // 10

// Function Composition (Pipe)
const pipe = (...fns) => (x) => fns.reduce((v, fn) => fn(v), x);
const addTen = x => x + 10;
const processNumber = pipe(double, addTen);
console.log(processNumber(5)); // (5 * 2) + 10 = 20