JavaScript Academy · Lesson

The Reflect API

Forward operations with Reflect methods.

Lesson 3 of 413 steps

The Reflect API is a free JavaScript Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

What Is Reflect?

Reflect is a built-in object with methods that perform the default low-level operations on objects: getting, setting, deleting, and more.

Its methods mirror the proxy traps one-to-one, which makes it the ideal partner for writing correct proxies.

Reflect.get

Reflect.get(target, prop) reads a property, the same as target[prop], but as a clean function call.

const obj = { name: 'Ada', age: 36 };

console.log(Reflect.get(obj, 'name'));
console.log(Reflect.get(obj, 'age'));

Reflect.set

Reflect.set(target, prop, value) writes a property and returns true or false indicating success, just like a set trap expects.

const obj = {};
const ok = Reflect.set(obj, 'score', 42);

console.log(ok);
console.log(obj.score);

Reflect.has and deleteProperty

Reflect.has mirrors the in operator, and Reflect.deleteProperty mirrors delete, both as functions returning booleans.

const obj = { a: 1, b: 2 };

console.log(Reflect.has(obj, 'a'));
console.log(Reflect.deleteProperty(obj, 'b'));
console.log(Reflect.has(obj, 'b'));

Reflect.ownKeys

Reflect.ownKeys returns all own property keys, including symbols and non-enumerable ones, more complete than Object.keys.

const obj = { a: 1, b: 2 };
Object.defineProperty(obj, 'hidden', { value: 3, enumerable: false });

console.log(Object.keys(obj));
console.log(Reflect.ownKeys(obj));

Forwarding in a Proxy

The real power: inside a trap, call the matching Reflect method to perform the default behavior, then add your custom logic around it.

const proxy = new Proxy({ v: 1 }, {
  get(target, prop, receiver) {
    console.log('reading ' + prop);
    return Reflect.get(target, prop, receiver);
  }
});

console.log(proxy.v);

Forwarding set Correctly

Using Reflect.set inside a set trap returns the right boolean automatically, so you do not have to remember to return true yourself.

const proxy = new Proxy({}, {
  set(target, prop, value, receiver) {
    console.log('writing ' + prop);
    return Reflect.set(target, prop, value, receiver);
  }
});

proxy.x = 10;
console.log(proxy.x);

Why the receiver Matters

Passing receiver to Reflect.get and Reflect.set ensures getters and setters run with the correct this, which matters when objects inherit from one another.

const base = { get label() { return 'id-' + this.id; } };
const proxy = new Proxy(base, {
  get(target, prop, receiver) {
    return Reflect.get(target, prop, receiver);
  }
});
const child = Object.create(proxy);
child.id = 7;
console.log(child.label);

Reflect.apply

Reflect.apply(fn, thisArg, argsArray) calls a function with a given this and an array of arguments, a cleaner alternative to fn.apply.

function greet(greeting, name) {
  return greeting + ', ' + name;
}

console.log(Reflect.apply(greet, null, ['Hello', 'Ada']));

Reflect.construct

Reflect.construct(Cls, argsArray) creates an instance, equivalent to new Cls(...args) but with an array of arguments.

class Point {
  constructor(x, y) { this.x = x; this.y = y; }
}

const p = Reflect.construct(Point, [3, 4]);
console.log(p.x, p.y);

Why Reflect Matters

Reflect gives function-form, predictable versions of object internals and pairs perfectly with proxies: each trap forwards to its matching Reflect method. The result is correct, minimal proxy code. Next you will apply all this to practical use cases.

const audited = new Proxy({ balance: 100 }, {
  set(t, p, v, r) {
    console.log(p + ': ' + t[p] + ' -> ' + v);
    return Reflect.set(t, p, v, r);
  }
});
audited.balance = 150;
console.log(audited.balance);

Quick Check

Why call Reflect.get(target, prop, receiver) inside a get trap instead of target[prop]?

Recap: The Reflect API

You learned the partner of Proxy:

  • Reflect provides function-form default operations: get, set, has, deleteProperty, ownKeys.
  • Its methods mirror proxy traps one-to-one.
  • Forwarding through Reflect with the receiver keeps inheritance correct.
  • Reflect.apply and Reflect.construct call and instantiate cleanly.
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “The Reflect API” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “The Reflect API”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

What will I learn in “The Reflect API”?

Forward operations with Reflect methods. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “The Reflect API” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating a Proxy
  2. get and set Traps
  3. The Reflect API
  4. Practical Proxy Use Cases
← Back to JavaScript Academy