All three of the call
, bind
, and apply
methods set the this
argument to the function.
call
and apply
methods set this
to a function and call the function.bind
method will only set this
to a function. We will need to separately invoke the function.The call
method binds the this
value to the function and executes the function. It takes the this
value and a list of arguments as parameters. Then, it returns the value returned by the function, which is called using the call
method.
function test(arg1, arg2){
console.log(this.num, arg1, arg2); // 100, 10, 20
}
test.call({num: 100}, 10, 20);
In the code above, we have called the test
function using the call
method. The call
method will invoke the test
function with the this
value as the object passed to the call
method.
The apply
method binds the this
value to the function and executes the function. It takes the this
value and a single array object as parameters, and it returns the value returned by the function, which is called using the apply
method.
function test(...arguments){
console.log(this.num, arguments);//100, [1,2,3]
}
test.apply({num: 100}, [1,2,3]);
In the code above, we have called the test
function using the apply
methodtest
functionthis
value as the object passed to the apply
method.
call()
andapply()
are identical in functionality, the only difference is thatcall()
accepts a list of arguments; whereas,apply()
accepts a single array of arguments.
The bind
method binds the this
value to the function and returns a new function. However, we still need to separately invoke the returned function.
function test(arg){
console.log(this.number, arg);
}
let bindedFn = test.bind({number: 99}, "argument");
bindedFn(); // 99, "argument"
In the above code, we bind the this
value for the test
function and invoke the returned function from the bind
method.
call
: binds the this
value, invokes the function, and allows you to pass a list of arguments.
apply
: binds the this
value, invokes the function, and allows you to pass arguments as an array.
bind
: binds the this
value, returns a new function, and allows you to pass in a list of arguments.