Understand the bind() function in Javascript


TheΒ .bind() function allows as to create a function by setting a custom this value. The bind function returns a function that is callable like a normal function and can also be passed to other functions.

When we need to go for bindΒ ?

Consider that we have an object:

window.Name  = "Window";
var JsJeep = {
    Name  : "Javascript Jeep πŸš™ πŸš—",
    greet : function() {

console.log(" πŸ‘‹ from " + this.Name);
    }
}

When we call the function greet with the JsJeep object:

JsJeep.greet(); //  πŸ‘‹from Javascript Jeep πŸš™ πŸš—

What if we store the function ina other variable and call the function:

var greet = JsJeep.greet;
greet(); //  πŸ‘‹ from Window.

The this points to "Window" because this inside the greet function is global window object, so we need to tell the browser specifically use JsJeep object that it should refer to. This is when bind is used.

Let’s see another example, then we understand the bind. When we do the same inside a setTimeout:

setTimeout(JsJeep.greet , 0); //  πŸ‘‹ from Window

Inside the setTimeout function the value of this will again be window because we are passing the reference of greet function to setTimeout, and the window object is executing the greet function call after 0 milliseconds.

The above setTimeout code is equivalent to

var func = JsJeep.greet;
setTimeout(func, 0); // which is similar to above case.

To solve the above problem, we use the bind function which allows us to specify the value of this inside a function.

Syntax

functionToBind.bind(ourCustomThisObject, argumentsToFunction) β†’ the additional arguments are optional.

Solution for above problems:

var fun = JsJeep.greet;
var newFun = fun.bind(JsJeep);
newFun(); //  πŸ‘‹ from Javascript Jeep πŸš™ πŸš—
setTimeout(newFun, 0); //  πŸ‘‹from Javascript Jeep πŸš™ πŸš—

So we got solved the problem.

Let’s see some tricky areas.

1. What will happen when we pass null instead of JsJeep object

 var fun = JsJeep.greet;
 var newFun = fun.bind(null); // then we are binding nothing
  • There are two cases if we use strict mode then the this will be nullΒ .
  • If we are in non-strict mode then the this will be window object.

2. We can’t change the this value once the bind is doneΒ .

var fun = JsJeep.greet;
var bindFun= fun.bind( {Name: "Balaji"} );

bindFun = bindFun.bind( {name: "Raju"} );
bindFun(); //  πŸ‘‹ from Balaji

The reason is that the bindFun has fixed this after the first invocation, and we can’t change that afterward.

3. How to find if a function is bound functionΒ .

The name property of a bound function is prefixed with β€œbound”

log(JsJeep.greet.name) // greet
var fun = JsJeep.greet;
var bindFun= fun.bind( JsJeep );
log(bindFun.name); // bound greet

4. If a property of the object is updated then the value of the property is also updated inside the bind function.

var JsJeep = {
    Name  : "Javascript Jeep πŸš™ πŸš—",
    greet : function() {

console.log(" πŸ‘‹ from " + this.Name);
   }
}
var fun = JsJeep.greet;
var bindFun= fun.bind( JsJeep );
JsJeep.Name = "New Jeep 🚘 "; //updating the property.

bindFun(); //   πŸ‘‹  from New Jeep 🚘

Notes

For writing ✍️ this post I πŸ€·πŸ»β€β™‚οΈ referred MDN and javascrip.info and some answers from StackOverflowΒ , thanks πŸ™ to them 🌟 πŸ’– 😜 .

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”

Learn new way of creating random numbers here

β€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”β€Šβ€”

If you find this helpful surprise 🎁 me here.

Share if you feel happy πŸ˜ƒ πŸ˜† πŸ™‚Β .

Follow Javascript JeepπŸš™ if you feel worthy.


https://gitconnected.com/learn/javascript

Leave a comment

Design a site like this with WordPress.com
Get started