C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript handler.apply() MethodThe handler.apply() method is used to trap for a function call. The value returned by the apply trap is also going to be used as the result of a function call through a proxy. Syntaxapply: function(target, thisArg, argumentsList) Parameterstarget: The target object. thisArg: thisArg use for the call. argumentsList: The list of arguments used for the call. Return valueThis method can return any value. Browser Support
Example 1var o = function(arg1, arg2) { document.writeln('proxy value(' + arg1 + ', ' + arg2 + ')'); }; var proxy = new Proxy(o, { apply: function(target, thisArg, parameters) { document.writeln('First exam..'); document.writeln(""); return target.apply(thisArg, parameters); } }); proxy('23', '54'); Output: First exam.. proxy value(23, 54) Example 2var str = function(arg1, arg2) { document.writeln('in x(' + arg1 + ', ' + arg2 + ')'); }; var proxy = new Proxy(str, { apply: function(target, thisArg, parameters) { document.writeln('in apply'); document.writeln(" Output: in apply in x(direct1, direct2) in apply in x(add, add) in apply in x(string, string) Example 3function p (a) { return a ; } var q = { apply: function(target, thisArg, argumentsList) { return target(argumentsList[0], argumentsList[1])*2; }}; var pro = new Proxy(p, q); document.writeln(p(56)); document.writeln(" Output: 56 68
Next TopicJavaScript handler
|