TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript handler apply() Method

JavaScript handler apply() Method with examples on javascript handler, javascript methods, javascript tutorial, apply(), construct(), defineProperty(), deleteProperty(), get(), setPrototypeOf(), set(), ownKeys(), isExtensible() etc.

<< Back to JAVASCRIPT

JavaScript handler.apply() Method

The 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.

Syntax

apply: function(target, thisArg, argumentsList)

Parameters

target: The target object.

thisArg: thisArg use for the call.

argumentsList: The list of arguments used for the call.

Return value

This method can return any value.

Browser Support

Chrome 49
Edge 12
Firefox 18
Opera 36

Example 1

 var 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');
Test it Now

Output:

First exam.. 
proxy value(23, 54)

Example 2

var 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("
"); return target.apply(thisArg, parameters); } }); proxy('direct1', 'direct2'); proxy.apply(null, ['add', 'add']); proxy.call(null, 'string', 'string');
Test it Now

Output:

in apply 
in x(direct1, direct2) in apply 
in x(add, add) in apply 
in x(string, string)

Example 3

function 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("
"); document.writeln(pro(34));
Test it Now

Output:

56 
68

Next TopicJavaScript handler




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf