Function.call() method
The first argument is the this value, the remaining arguments are passed into the function.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
function sum(num1, num2){
return num1 + num2;
}
function callSum(num1, num2){
return sum.call(this, num1, num2);
}
document.writeln(callSum(10,10)); //20
window.color = "A";
var o = { color: "B" };
function sayColor(){
document.writeln(this.color);
}
sayColor(); //A
sayColor.call(this); //A
sayColor.call(window); //A
sayColor.call(o); //B
</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()