# Call, Apply, and Bind methods
Calling a function can be accomplished by writing the name of the function followed by one of these three methods.
The difference between them is that bind
creates a copy of the function with the new context for you tto use later. call
and apply
will both call the function immediately with the new context.
function f1(a, b) {
console.log(this);
return a + b;
}
let answer1 = f1.call(window, 10, 5);
// returns 15 (parameters passed separately)
// will console.log the Window Object
let answer2 = f1.apply(document, [10, 5]);
// returns 15 (parameters passed in an array)
// will console.log the document Object
let answer3 = f1.bind(document.body);
//answer3 is now a copy of the function f1
// when we run answer3, document.body will be the value for `this`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14