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. In your examples you could rewrite it as follows: function test() { console.log(this); } and call it as follows: test.apply(this); THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. Standard: ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Function.prototype.apply' in that specification.
We can use the array literal or array object to specify the parameters to the function.
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.
Die Methode apply ruft eine Funktion mit einem gegebenen this-Wert und einer beliebigen Anzahl Argumenten auf. Syntax apply() Parameters. The apply() method of a Function takes in:. By array-like object, we mean that any object that can have the length property to it such as NodeList or any of the custom object like the JSON format such as { ‘length’: 2, ‘0’: ‘javascript’, ‘1’: ‘MySQL’ } are also supported. 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.
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 definition of 'Function.prototype.apply' in that specification. In fact, JavaScript functions come with several methods built into Function.prototype.
Active 1 year, 3 months ago. It returns the result of the calling function along provided this value and arguments.Let's see an example to determine the maximum element.Let's see an example to determine the minimum element.Let's see an example to join array of different type.JavaTpoint offers too many high quality services. First let's take a look at call. Die Argumente für den Funktionsaufruf werden in einem Array oder Array-ähnlichen Objekt an die Methode übergeben.
; argsArray (Optional) - An Array-like object containing the arguments to the function. Developed by JavaTpoint.
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. All rights reserved.
Viewed 57k times 36. The second is an array of parameters for that function. apply takes two arguments: test.apply(null, [body]); The first argument sets the value of this when the function is executed. 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. function add (a, b) {return a + b; } // Outputs: 3 console. thisArg - The value of this provided for the call to func.If function is in non-strict mode, null or undefined values will be replaced by the global object and primitive values will be boxed. ALL RIGHTS RESERVED.
There is no need to rewrite the same function for multiple objects. log (add (1, 2)); // Outputs: 3 console. 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. call (this, 1, 2)); Assuming that you're not using strict mode, these invocations of add are exactly equivalent. The versions of the browsers that do not support the array-like objects as parameter throw an exception.
Living Standard