JavaScript Bind/Call/Apply
If you ever worked with event listeners in JavaScript, then chances are that you used either bind, call, or apply. Those three methods are very powerful functions that can be used within objects to set the this value explicitly. The three methods are very similar in terms of how they operated with each methods being unique for different cases.
Here, we have an example of bind being used in the case of an event listener. In the bold text, we add an event listener of click, then pass on the callback function of onSubmit. However, the event is currently pointing to the DOM object which will return an error when the event is being called. This when we can explicitly tell the onSubmit that we want to point this function to our current object when App is initialize.
class App {
constructor () {
this.textBox = document.querySelector('#todo-text')
this.submitBtn = document.querySelector('button')
this.submitBtn.addEventListener('click', this.onSubmit.bind(this))
this.todoList = new TodoList()
this.todoContainer = document.querySelector('.list')
}
onSubmit () {
event.preventDefault()
this.todoList.addTodo(this.textBox.value)
this.textBox.value = ''
this.render()
}
}If this seems confusing at first, not to worry! Let’s look at a much more simplified example.
var obj = {Pennsylvania: "Harrisburg"}function stateAndCapitals(year, population ) {
console.log("The capital of Pennsylvania is " +this.Pennsylvania + " which was founded in " + year, "and has a population of "+ population + ".")
}var test = stateAndCapitals.bind(obj, 1812, 48,904)
test()
// The capital of Pennsylvania is Harrisburg which was founded in 1812 and has a population of 48,904.
We have set the variable obj to an object containing the key and value of “Pennsylvania and Harrisburg. In our stateAndCapitals function, we print out “The capital of Pennsylvania is “ +this.Pennsylvania…etc. Note, this. Pennsylvania. This is pointing to our current object(which will be our obj variable later on) then we call our key from the object which give the value of Pennsylvania. Finally, in our test variable we bind the stateAndCapital to the obj variable so it will know where this is pointing to. Note that we call our test function at the end, this is due to bind not invoking the function immediately. Now what if we wanted it invoke our function at the same time we bound the function? This is where the call method comes in!
Get Brett Schules’s stories in your inbox
Join Medium for free to get updates from this writer.
The call method is does the same thing as bind expect for invoking our function right away.
stateAndCapitals.call(obj)
// The capital of Pennsylvania is Harrisburg which was founded in 1812 and has a population of 48,904.Finally, we have the apply method which just like out call method, however the apply method allows you to pass additional arguments in an array format. For example, passing in an array containing the elements “1812” and “48,904” will give you the same output as our bind and call methods.
stateAndCapitals.apply(obj, ["1812", "48,904"])
//// The capital of Pennsylvania is Harrisburg which was founded in 1812 and has a population of 48,904.There we have it!





