Proxy apply is used to trap a function invocation.
It takes three arguments:
The following code are applying a season discount on the final billing amount using the apply trap handler:
function getBill(amount) { return amount; } const billHandler = { /*from w w w . j ava 2s . co m*/ apply: function(target, context, args) { console.log("Applying Discount of 35%"); return args[0] - (args[0] * 0.35); } } const billProxy = new Proxy(getBill, billHandler); console.log(billProxy(300));
Since the target object here is a function, these are also called as function traps.
You can alternatively have traps for .call() and .bind() methods also.