Using Proxy for Object Operations Interception in Javascript


Create a proxy for an object to intercept and redefine fundamental operations (e.g., property lookup, assignment, enumeration, function invocation).

Source Code

let handler = {
  get: function(obj, prop) {
    return prop in obj ? obj[prop] : 37;
  }
};
let p = new Proxy({}, handler);
p.a = 1;
p.b = undefined;
console.log(p.a, p.b); // 1, undefined
console.log('c' in p, p.c); // false, 37
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments