
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
nullinstead ofJsJeepobject
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
thiswill benullΒ . - If we are in non-strict mode then the
thiswill bewindowobject.
2. We canβt change the
thisvalue once thebindis 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.