Skip to content
First let's take a look at call. The complete list of browsers and their supporting versions is given at the end of the article.Consider a simple example to get a hold on the sayings mentioned above.We can observe that the company1 records are fetched and the “this” keyword references to the company1 object because we have mentioned it as the first parameter. Mail us on hr@javatpoint.com, to get more information about given services. Similarly, if we would have usedinstead of company1 we would have got the following output because now the “this” will refer to the company2 object.We can use the apply method whose full name is Function.prototype.apply() for creating the functions that can be used by multiple objects by simply inheriting them in other objects.
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). However, there is one difference between the two. Uses the new array object as the argument.Since the fifth edition of the ECMAScript, the array-like objects can also be used to specify the arguments. Please mail your requirement at hr@javatpoint.com. In fact, JavaScript functions come with several methods built into Function.prototype. Applying a function to each object in a JavaScript array. The second is an array of parameters for that function. Developed by JavaTpoint. The JavaScript Function apply() method is used to call a function contains this value and an argument contains elements of an array. The only difference between both of them is that the call() method accepts the list of arguments in the comma-separated fashion while the apply() method wants an argument array. log (add (1, 2)); // Outputs: 3 console. The versions of the browsers that do not support the array-like objects as parameter throw an exception. Javascript apply is the method that helps us to call a function that sets the passed value or the object that is specified as the “this” keyword which is referred to as the current referencing object whenever the apply method is called and the array of the arguments that you want to pass to that function as the second parameter. The value of “this” can be specified at the time of the calling of a function.The call() and apply() method work in the same manner.
Javascript apply is the method that helps us to call a function that sets the passed value or the object that is specified as the “this” keyword which is referred to as the current referencing object whenever the apply method is called and the array of the arguments that you want to pass to that function as the second parameter. you may also have a look at the following articles to learn more –JavaScript Training Program (39 Courses, 23 Projects)© 2020 - EDUCBA. In your examples you could rewrite it as follows: function test() { console.log(this); } and call it as follows: test.apply(this); © Copyright 2011-2018 www.javatpoint.com. Viewed 57k times 36. Function.prototype.apply(thisArg, argArray) Attribute Writable : true Enumerable : false Configurable : true Beschreibung . The call() method accepts the arguments in the comma-separated format while the apply() method expects the argument list to be specified in the array format as the second argument of the function call.We can see that after using apply for your function it results in the calling of the function with the specified argument to be referenced by the “this” keyword and the arguments are passed to the function as specified in the array in the second parameter. The syntax of the apply function is as defined below –The apply() method helps us to write the function once and make the use that function by inheriting it by multiple objects. ; argsArray (Optional) - An Array-like object containing the arguments to the function. Unlike call() method, it contains the single array of arguments. There is no need to rewrite the same function for multiple objects.
Syntax