Use apply function to call a function in JavaScript
Description
The following code shows how to use apply function to call a function.
Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function sum(num1, num2){<!-- www . j a v a 2 s. co m-->
return num1 + num2;
}
function callSum1(num1, num2){
return sum.apply(this, arguments);
//passing in arguments object
}
function callSum2(num1, num2){
return sum.apply(this, [num1, num2]); //passing in array
}
document.writeln(callSum1(10,10)); //20
document.writeln(callSum2(10,10)); //20
</script>
</head>
<body>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Definition
Call a function defined later in JavaScript
Call a function in hyperlink in JavaScript
Call function inside a function in JavaScri...
Call your function in JavaScript
Call your function in body onLoad event in ...
Convert function to string in JavaScript
Create a function in JavaScript
Create a function variable in JavaScript
Create a function with Function constructor...
Use Bind to create a new new function whose...
Use function.call() to call a function in J...
Use function caller property in JavaScript
Use function reference value passing in Jav...
Use function to calculate average in JavaSc...
Use same name for different functions in Ja...
Use this to reference function context in J...
Use typeof to check function variable in Ja...
Call a function defined later in JavaScript
Call a function in hyperlink in JavaScript
Call function inside a function in JavaScri...
Call your function in JavaScript
Call your function in body onLoad event in ...
Convert function to string in JavaScript
Create a function in JavaScript
Create a function variable in JavaScript
Create a function with Function constructor...
Use Bind to create a new new function whose...
Use apply function to call a function in Ja...
Use function as variable in JavaScriptUse function.call() to call a function in J...
Use function caller property in JavaScript
Use function reference value passing in Jav...
Use function to calculate average in JavaSc...
Use same name for different functions in Ja...
Use this to reference function context in J...
Use typeof to check function variable in Ja...