Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Reflect is a built-in JavaScript object that provides methods for interceptable operations. It mirrors Proxy traps and offers a cleaner, more functional approach to meta programming. All Reflect methods are static functions - no constructor!
delete obj.prop or 'prop' in obj). It returns boolean success values instead of throwing errors, making code safer and more predictable!const obj = { x: 1 };
// Property operations
obj.x; // Get
obj.x = 2; // Set
delete obj.x; // Delete
'x' in obj; // Has
// Function operations
func.apply(obj, args);
new Constructor(...args);
// Issues:
// - Imperative syntax
// - Mixed patterns
// - No consistent APIconst obj = { x: 1 };
// Property operations
Reflect.get(obj, 'x');
Reflect.set(obj, 'x', 2);
Reflect.deleteProperty(obj, 'x');
Reflect.has(obj, 'x');
// Function operations
Reflect.apply(func, obj, args);
Reflect.construct(Constructor, args);
// Benefits:
// - Functional approach
// - Consistent API
// - Returns boolean successGets property value
Alternative to: obj[prop]
Sets property value
Alternative to: obj[prop] = value
Checks property existence
Alternative to: 'prop' in obj
Deletes property
Alternative to: delete obj.prop
Calls function
Alternative to: func.apply()
Creates instance
Alternative to: new Constructor()
Property access and assignment
Check existence and delete properties
Call functions with specific context
Alternative to 'new' operator
Define property with descriptor
Get property descriptor
All own keys (strings + symbols)
Get object's prototype
Set object's prototype
Check/prevent object extension
Working with property descriptors
Get all own property keys
this binding, and ensure correct behavior with getters/setters and inheritance. Always use Reflect instead of direct operations!const proxy = new Proxy(obj, {
get(target, prop) {
console.log('Getting', prop);
return target[prop]; // Wrong!
},
set(target, prop, value) {
console.log('Setting', prop);
target[prop] = value; // Wrong!
return true;
}
});
// Issues:
// - Doesn't handle receiver
// - Breaks getter/setter logic
// - Inheritance problemsconst proxy = new Proxy(obj, {
get(target, prop, receiver) {
console.log('Getting', prop);
return Reflect.get(target, prop, receiver);
},
set(target, prop, value, receiver) {
console.log('Setting', prop);
return Reflect.set(target, prop, value, receiver);
}
});
// Benefits:
// - Correct receiver handling
// - Proper getter/setter behavior
// - Works with inheritanceType validation using Proxy + Reflect
Prevent errors with non-existent properties
Reflect provides functions for operations
Cleaner, more consistent than mixed syntax
Returns true/false for success
No errors thrown, safer code
13 methods match Proxy traps exactly
Perfect for Proxy handlers
Always use in Proxy handlers
Ensures correct receiver behavior