Difference between Proxy constructor and Reflect() in js
-
What is the difference between proxy constructor and Reflect in javascript?
Reflect()
let obj = {name: 'John Doe', age: 30};
// Using Reflect.get
let name = Reflect.get(obj, 'name');
console.log(name); // Output: John Doe
// Using Reflect.set
Reflect.set(obj, 'age', 31);
console.log(obj.age); // Output: 31
Proxy Object()
const handler = {
get(target, prop, receiver) {
return `Custom getter for ${prop}`;
}
};
const target = { name: 'Alice' };
const proxy = new Proxy(target, handler);
console.log(proxy.name); // Outputs: Custom getter for name
1 Answer
11 months ago by Ahammed Ali