Function apply()
The apply() method accepts two arguments:
- the value of this
- an array of arguments.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
function sum(num1, num2){
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>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Function:
- The Function Type
- Function Declarations versus Function Expressions
- Functions as Values
- Returning a function from a function
- Function arguments
- this for function context
- Function caller
- Function length property
- Function apply()
- Function.call() method
- Function's bind() method
- Function toLocaleString() and toString()