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
// 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
Parameter
Description
target
The original function that the Proxy wraps.
thisArg
The value of this for the call. It is undefined for a plain call like proxy().
argumentsList
A real array with the arguments of the call. It is empty when there are none.
Return Value
Return Value
Description
Any value
Whatever 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().
A function proxy.new Proxy(add, ...) makes a callable object. Calling tracked(2, 3) runs the apply trap with args as [2, 3].
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.
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.
pong
pong
Third call: Limit reached
Calls counted: 3
Direct call: pong
How It Works
Count each call. Every use of limited() runs the trap, and the counter goes up by one.
Stop at the limit. On the third call, the trap throws an error and never reaches Reflect.apply(). The caller receives the error.
Direct calls bypass it. The original ping() is still available without a limit, which is why you should only share the proxy.
Compatibility
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.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox49+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo 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.