call() vs apply() vs bind() in JavaScript
The call(), apply(), and bind() methods allow you to change the runtime binding of a function’s this
keyword from the function’s lexical object to a different object of your choice.
This article will show you how to use the three methods to reassign any function (or method) from one object to another.
Let’s begin by understanding how method reassignment works.
How Does Method Reassignment Work?
By default, the owner (lexical context) of a method is the object encasing it.
For instance, consider this code:
In the snippet above, the showMyName()
method’s default owner is the firstName
object because firstName
is the container housing showMyName()
.
Therefore, when firstName.showMyName()
got invoked, showMyName
’s this
keyword referenced firstName
.
But how can we make showMyName
’s this
keyword reference lastName
instead? Should we redefine showMyName
in lastName
?
Well, this is precisely where method reassignment comes in handy.
call()
, apply()
, and bind()
allows you to create a method once and reuse it in different other objects—without rewriting the method.
So, for instance, instead of redefining showMyName
in lastName
, you can simply use either call()
, apply()
, or bind()
to reassign showMyName
to lastName
like so:
In the snippet above, we used apply()
to reassign showMyName
to lastName
.
Consequently, when firstName.showMyName.apply(lastName)
got invoked, showMyName
’s this
keyword referenced lastName
.
Therefore, firstName.showMyName.apply(lastName)
’s invocation correctly returned "Sofela"
.
So how exactly does the method call()
, apply()
, and bind()
work?
Let’s find out by taking a closer look at each method, starting with call()
.
What Is the call()
Method?
call() is one of JavaScript’s built-in methods that you can use to reassign a specific method from one object to a different one.
Here’s an example:
In the snippet above, "Oluwatobi Sofela"
returned because we used call()
to assign fullName()
to myNames
.
Therefore, fullName()
’s this
keyword referenced myNames
object.
For simplicity, you can read myFullName.fullName.call(myNames)
as:
“In myFullName
object, get the fullName
method and call
it inside myNames
object.”
So now that you know what call()
is, we can talk about its syntax.
call()
’s syntax
call()
accepts one or more optional arguments.
The first argument you pass into call()
will refer to the object you want to assign a method.
If you specify any additional arguments, call()
will read them as the parameter values you wish to pass to the method you are reusing.
For instance, consider the code below:
In the snippet above, we passed three arguments to the call()
method.
The first argument (myNames
) is the object to which we want to assign aboutComp
.
The other two arguments (new Date().getFullYear() - 2020
and "www.codesweetly.com"
) are the parameter values we want to pass to aboutComp
.
Let’s now see how apply()
works.
What Is the apply()
Method?
apply() is one of JavaScript’s built-in methods that you can use to reassign a specific method from one object to a different one.
apply()
does the same thing as call()
. The core difference between the two methods is that apply()
accepts only two arguments. In contrast, call()
takes as many arguments as you need.
Let’s take a closer look at its syntax.
apply()
’s syntax
apply()
accepts only two arguments. The first is required, while the second is optional.
The first argument apply()
accepts is the object you wish to assign a specified method.
The second argument is the array of values you wish to pass into the method you are reusing.
Here’s an example:
In the snippet above, we passed in two arguments to the apply()
method.
Learn CSS Grid with Images
The first argument (myNames
) is the object to which we want to assign aboutComp
.
The second argument ([new Date().getFullYear()—2020, "www.codesweetly.com"]
) is the array we want to pass to aboutComp
.
For simplicity, you can read myBio.aboutComp.apply(myNames, [new Date().getFullYear() - 2020, "www.codesweetly.com"])
as:
“In myBio
object, get the aboutComp
method and apply
it in myNames
object while using [new Date().getFullYear() - 2020, "www.codesweetly.com"]
as aboutComp
’s argument.”
Let’s now see how bind()
works.
What Is the bind()
Method?
bind() is one of JavaScript’s built-in methods that you can use to reassign a specific method from one object to a different one.
bind()
does the same thing as call()
. The main difference is that bind()
does not automatically invoke the method on which it got used. Instead, it only binds (assigns) the method to the new object.
In contrast, call()
automatically invokes the method on which you used it.
Let’s take a closer look at its syntax.
bind()
’s syntax
bind()
accepts one or more optional arguments.
The first argument you pass into bind()
will refer to the object to which you want to assign a method.
If you specify any other arguments, bind()
will read them as the parameter values you wish to pass to the method you are reusing.
Here’s an example:
Notice that bind()
returned the aboutPal()
function—without invoking it. The invocation did not happen because bind()
—unlike call()
—does not automatically invoke the method on which you used it.
If you want bind()
to invoke the method, you must explicitly instruct it to do so by placing a set of parentheses ()
after the bind command like so:
For simplicity, you can read myBio.aboutComp.bind(myNames, new Date().getFullYear() - 2020, "www.codesweetly.com")
as:
“In myBio
object, get the aboutComp
method and bind
it in myNames
object while using new Date().getFullYear()—2020
and "www.codesweetly.com"
as aboutComp
’s arguments.”