| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
apply Method (Function) (JavaScript) | Microsoft Docs |
01/18/2017 |
windows-client-threshold |
|
language-reference |
|
|
|
b36df78e-b14b-46ca-b5cb-de752d80f40a |
10 |
mikejo5000 |
mikejo |
ghogen |
Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function.
apply([thisObj[,argArray]])
thisObj
Optional. The object to be used as the this object.
argArray
Optional. A set of arguments to be passed to the function.
If argArray is not a valid object, then an "Object expected" error occurs.
If neither argArray nor thisObj are supplied, the original this object is used as thisObj and no arguments are passed.
The following code shows how to use the apply method.
function callMe(arg1, arg2){
var s = "";
s += "this value: " + this;
s += "<br />";
for (i in callMe.arguments) {
s += "arguments: " + callMe.arguments[i];
s += "<br />";
}
return s;
}
document.write("Original function: <br/>");
document.write(callMe(1, 2));
document.write("<br/>");
document.write("Function called with apply: <br/>");
document.write(callMe.apply(3, [ 4, 5 ]));
// Output:
// Original function:
// this value: [object Window]
// arguments: 1
// arguments: 2
// Function called with apply:
// this value: 3
// arguments: 4
// arguments: 5
[!INCLUDEjsv55]