Bind

Bind allows us to copy a function. Why would we want to do this? By copying it, we change the context of “this” for the entire new function. Here is an example…

let animal = {
    animalName: ‘Bongo’,
    getName: function(){
        return this.animalName;
    }
};
let newAnimalName = { animalName: ‘George’ };
let newAnimal = animal.getName.bind(newAnimalName); console.log(newAnimal()); 

The output would be “George”. This was possible because we used bind to copy the original function to the newAnimal function. This also allowed us to change the “this” context to newAnimalName at the same time.

Happy Coding!

Clay Hess

More To Explore

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.