Code Logo

Intercept Access with Proxy

Published at25 Jul 2026
JavaScript Functions Hard 0 views
Like0

Write a JavaScript function that creates a Proxy around a target object. The Proxy intercepts get operations to return the property value doubled, and intercepts set operations to log the action without actually storing the value. Return the Proxy-wrapped object.

The Proxy object in JavaScript enables custom behavior for fundamental operations on objects. A Proxy wraps a target object and intercepts operations via handler functions (traps). Common traps include get (property access), set (property assignment), has (in operator), deleteProperty, and apply (function call).

Proxies are a powerful metaprogramming feature unique to JavaScript. They enable use cases like validation, logging, profiling, access control, and reactivity (as used in Vue 3). Unlike getters/setters which work on individual properties, Proxies can intercept ALL operations on an object without modifying the original object.

Time complexity is O(1) per trapped operation. The get and set traps add minimal overhead compared to direct property access. Proxies are transparent — code using the proxied object generally cannot tell it's a Proxy.

Edge cases include intercepting non-existent properties (get returns undefined doubled = NaN, set still logs), prototype chain lookups, and ensuring the set trap returns true to indicate successful assignment (required by the Proxy specification).

Example Input & Output

Example 1
Input
{"b":7}
Output
14
Explanation

7*2=14

Example 2
Input
{"z":0}
Output
0
Explanation

0*2=0

Example 3
Input
{"x":5}
Output
10
Explanation

Accessing x returns 5*2=10

Example 4
Input
{"y":10}
Output
20
Explanation

Accessing y returns 10*2=20

Example 5
Input
{"a":3}
Output
6
Explanation

3*2=6

Algorithm Flow

Recommendation Algorithm Flow for Intercept Access with Proxy
Recommendation Algorithm Flow for Intercept Access with Proxy

Solution Approach

function solution(target) {
  return new Proxy(target, {
    get: function(obj, prop) {
      return obj[prop] * 2;
    },
    set: function(obj, prop, value) {
      console.log('Setting ' + prop + ' to ' + value);
      return true;
    }
  });
}

Best Answers

javascript - Approach 1
function solution(target) {
  return new Proxy(target, {
    get: function(obj, prop) {
      return obj[prop] * 2;
    },
    set: function(obj, prop, value) {
      console.log('Setting ' + prop + ' to ' + value);
      return true;
    }
  });
}