JavaScript Proxy handler.apply() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Proxy trap

Definition

handler.apply(target, thisArg, argumentsList) is a trap that runs when someone calls a Proxy. It only works when the target of the Proxy is a function. Instead of running the function straight away, JavaScript first runs your trap.

Inside the trap you can log the call, check or change the arguments, change the result, or block the call. To run the original function, use Reflect.apply(target, thisArg, argumentsList).

Remember
const handler = {
  apply(target, thisArg, argumentsList) { ... }
};

target         → the original function
thisArg        → the this value of the call
argumentsList  → an array with the arguments

return value   → becomes the result of the call

Syntax

Wrap a function in a Proxy and add an apply trap to the handler.

JavaScript
function add(a, b) {
  return a + b;
}

const loud = new Proxy(add, {
  apply(target, thisArg, args) {
    console.log("Called with " + args.join(", "));
    return Reflect.apply(target, thisArg, args);
  }
});

console.log(loud(2, 3));   // Called with 2, 3 and then 5
CallWhat it does
proxy(1, 2)A normal call of the Proxy.
proxy.call(thisArg, 1, 2)Calling with call() also runs the trap.
proxy.apply(thisArg, [1, 2])Calling with apply() also runs the trap.
Reflect.apply(proxy, thisArg, [1, 2])The function-style call. It runs the trap too.

A standard JavaScript Proxy trap (ES2015). You write it inside the handler object that you pass to new Proxy(). JavaScript calls it for you. No import required. Siblings: Proxy.revocable(), handler.construct(), handler.defineProperty(), handler.deleteProperty(), handler.get(), handler.getOwnPropertyDescriptor(), handler.getPrototypeOf(), handler.has(), handler.isExtensible(), handler.ownKeys(), handler.preventExtensions(), handler.set(), handler.setPrototypeOf().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
handler.apply(target, thisArg, argumentsList)

// target        : the original function
// thisArg       : the value of this for the call
// argumentsList : an array of the arguments
// returns       : any value (the result of the call)

Parameters

ParameterDescription
targetThe original function that the Proxy wraps.
thisArgThe value of this for the call. It is undefined for a plain call like proxy().
argumentsListA real array with the arguments of the call. It is empty when there are none.

Return Value

Return ValueDescription
Any valueWhatever the trap returns becomes the result of the call. Usually this is the result of Reflect.apply(), but it can be anything.
TypeError (thrown)The Proxy was made from something that is not a function, so the call fails before the trap runs.

JavaScript Examples

Example 1 — Log Every Call of a Function

Wrap a function so that every call writes a note, and then runs the original function with Reflect.apply().

JavaScript
function add(a, b) {
  return a + b;
}

const logs = [];
const tracked = new Proxy(add, {
  apply(target, thisArg, args) {
    logs.push("add(" + args.join(", ") + ")");
    return Reflect.apply(target, thisArg, args);
  }
});

console.log("Result: " + tracked(2, 3));
console.log("Result: " + tracked(10, 5));
console.log("Log: " + logs.join(" | "));
console.log("typeof: " + typeof tracked);
Try It Yourself

How It Works

  1. A function proxy. new Proxy(add, ...) makes a callable object. Calling tracked(2, 3) runs the apply trap with args as [2, 3].
  2. Log, then forward. The trap saves a note and returns Reflect.apply(target, thisArg, args), which runs the real add function. The caller gets the normal result.
  3. Still a function. typeof tracked is "function", so other code can treat the proxy like the original.

Example 2 — Change Arguments and Results

The trap can fix the input before the call and change the output after it. Here, a missing argument gets a default and the answer is rounded.

JavaScript
function area(width, height) {
  return width * height;
}

const safeArea = new Proxy(area, {
  apply(target, thisArg, args) {
    const width = args[0] === undefined ? 1 : args[0];
    const height = args[1] === undefined ? 1 : args[1];
    const result = Reflect.apply(target, thisArg, [width, height]);
    return Math.round(result * 100) / 100;
  }
});

console.log("Both: " + safeArea(2, 3));
console.log("One missing: " + safeArea(4));
console.log("None: " + safeArea());
console.log("Decimals: " + safeArea(1.234, 2));
console.log("Original: " + area(4));
Try It Yourself

How It Works

  1. Fix the input. The trap reads args[0] and args[1] and replaces a missing value with 1 before calling the real function.
  2. Fix the output. The result is multiplied by 100, rounded and divided by 100, so 1.234 * 2 (2.468) becomes 2.47.
  3. Original untouched. The plain area(4) has no trap, so it multiplies 4 by undefined and gives NaN.

Example 3 — Block Calls and Count Them

A trap can decide to not call the function. This proxy allows only two calls, and then throws an error. The catch block prints the error message.

JavaScript
function ping() {
  return "pong";
}

let calls = 0;
const limited = new Proxy(ping, {
  apply(target, thisArg, args) {
    calls++;
    if (calls > 2) {
      throw new Error("Limit reached");
    }
    return Reflect.apply(target, thisArg, args);
  }
});

console.log(limited());
console.log(limited());

try {
  limited();
} catch (err) {
  console.log("Third call: " + err.message);
}

console.log("Calls counted: " + calls);
console.log("Direct call: " + ping());
Try It Yourself

How It Works

  1. Count each call. Every use of limited() runs the trap, and the counter goes up by one.
  2. Stop at the limit. On the third call, the trap throws an error and never reaches Reflect.apply(). The caller receives the error.
  3. Direct calls bypass it. The original ping() is still available without a limit, which is why you should only share the proxy.

Browser & Runtime Support

handler.apply() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.

✓ ES2015 · Widely supported

handler.apply()

Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.

ES2015 Standard since
Google Chrome 49+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium and legacy
Full support
Mozilla Firefox 49+ · Desktop & Mobile
Full support
Apple Safari 10+ · macOS & iOS
Full support
Opera 36+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 49+ · Chromium based
Supported
Node.js 6+ · Server runtime
Supported
Deno Supported · JavaScript runtime
Supported
Bun Supported · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
handler.apply() Excellent

Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.

Frequently Asked Questions

It is a method of the handler object that runs when a Proxy of a function is called. It receives the original function, the this value and an array of arguments, and its return value becomes the result of the call.
No. A Proxy is only callable when its target is a function. If the target is a plain object, calling the Proxy throws a TypeError, whatever traps you add.
Use Reflect.apply(target, thisArg, args). It calls the original function with the same this value and arguments, and returns its result, which you can then return from the trap.
Yes. proxy(1, 2), proxy.call(thisArg, 1, 2), proxy.apply(thisArg, [1, 2]) and Reflect.apply(proxy, thisArg, [1, 2]) all run the trap.
The apply trap is code you write that intercepts a call. Reflect.apply() is a function that actually makes a call. They are often used together: the trap decides what to do, and Reflect.apply() does the normal call.

Next: handler.construct()

Learn how a Proxy can watch and change object creation with the construct trap.

handler.construct() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful