R
Rishtaara
JavaScript Fundamentals
Lesson 22 of 28Article16 min

Meta, Proxy & Typed Arrays

Proxy wraps an object and lets you run code when someone reads or writes a property. Simple use: logging, validation, or default values.

Proxy — get and set traps

Proxy wraps an object and lets you run code when someone reads or writes a property. Simple use: logging, validation, or default values.

Real-life example: A security guard at a building door (Proxy) checks every visitor (get/set) before they enter or leave a room (target object).

Proxy with get and set traps
const user = { name: "Asha", score: 0 };

const tracked = new Proxy(user, {
  get(target, key) {
    console.log("Reading:", key);
    return target[key];
  },
  set(target, key, value) {
    if (key === "score" && value < 0) {
      throw new Error("Score cannot be negative");
    }
    target[key] = value;
    return true;
  },
});

tracked.score = 10;
console.log(tracked.name);

ArrayBuffer — raw binary memory

ArrayBuffer holds raw bytes in memory. You cannot read it directly — you view it through typed arrays.

Real-life example: ArrayBuffer is like an empty warehouse floor. Typed arrays are labeled shelves (8-bit, 16-bit) that tell you how to read each box.

Create ArrayBuffer
// 16 bytes of raw memory
const buffer = new ArrayBuffer(16);
console.log(buffer.byteLength); // 16

Uint8Array — when to use typed arrays

Typed arrays like Uint8Array read/write numbers in a fixed format. Use them for images, audio, files, WebGL, and network binary data.

Real-life example: A digital photo is millions of byte values for red, green, blue. Uint8Array lets JavaScript work with that raw pixel data quickly.

Uint8Array example
const buffer = new ArrayBuffer(4);
const bytes = new Uint8Array(buffer);

bytes[0] = 255;
bytes[1] = 128;
bytes[2] = 64;
bytes[3] = 0;

console.log(bytes); // Uint8Array [255, 128, 64, 0]