25
loading...
This website collects cookies to deliver better user experience
ReactJS
. While learning about class based components I stumbled across the bind
keyword.this.somefunction = this.somefunction.bind(this)
call, apply and bind
which basically do similar things, we need to understand the this
keyword in JavaScript.this
?this
in global scopethis
within an objectthis
within a functionthis
within classesthis
keyword will point to something according to the context it is in or we can point it to the thing we want using call, apply and bind . This will make better sense when we see the different contexts this
can be defined in. this
keyword in a global context ( outside any kind of function ), by default it points to the window
object.this.thing = "window thing"
this.thing
as well as window.thing
since this
points to the window object.console.log(this.thing)
// # "window thing"
console.log(window.thing)
// # "window thing"
"use strict"
in the JS file .function printsomething(){
console.log(this) // Window object
}
printsomething()
this
in a function in non-strict mode it points to the window or the global object. In strict mode this
is undefined and it looses the context."use strict"
function printsomething(){
console.log(this) // undefined
}
printsomething()
this
keyword in an object method, this
points to the object it was defined in. this.name = "Some other movie"
const movie = {
name : "Peanut Butter Falcon",
print : function (){
console.log(this.name)
}
}
movie.print()
// OUTPUT
// Peanut Butter Falcon
print
within the object movie
. The object movie
has a property called name
.print
we call console.log(this.name)
which would basically point to the name
property of the movie
object.print
function is in the context of the object movie
.this
depends on the way it was called?print
to another variable called globalprint
?this.name = "Rocky"
const movie = {
name : "Peanut Butter Falcon",
print : function (){
console.log(this.name)
}
}
const globalprint = movie.print
globalprint()
//output : Rocky
this
has changed to the global context since globalprint
is not a method of the movie
object, it is an independent function as we have seen in the function section, points to the global context in non-strict mode
and is undefined in strict mode
."Rocky"
instead of "Peanut Butter Falcon"
.this.name = "Rocky"
const movie = {
name : "Peanut Butter Falcon",
print : function (){
console.log(this.name)
}
}
setTimeout(movie.print , 1000 ); // Output : Rocky
function setTimeout(callback, delay){
//wait for 'delay' milliseconds
callback();
}
setTimeout( movie.print, 1000 );
movie.print
to it's callback argument.callback = movie.print
print
will be "undefined" in strict mode
and in non-strict mode
class theatre {
constructor(person,movie){
this.person = person
this.movie = movie
}
display(){
console.log(`${this.person} is watching ${this.movie}`)
}
}
const peter = new theatre("peter","interstellar")
const jack = new theatre("jack","inception")
jack.display()
//output : peter is watching interstellar
peter.display()
//output : jack is watching inception
this
point to the current instance of the class.jack
and peter
.display
will lose context if passed as a callback function or assigned to another variable like we saw in the functions section.strict mode
.const callback = jack.display
callback() // "this" will be undefined
bind
method shortly to fix this problem. To give you an idea , bind
will basically glue value of this
to the display function and it will always be called in that context, fixing the problem of the context getting lost.this
keyword to point rather than JavaScript assigning it for you.call
method is used to call a function with the this
pointing to a thing of your choice.call(thisArg, arg1, ... , argN)
thisArg
helps the call function to knw on which this
should the function be called.const movie = {
name : "Peanut Butter Falcon",
print : function (){
console.log(this.name)
}
}
const movie1 = {
name : "Peanut Butter Falcon",
print : function (){
console.log(this.name)
}
}
const movie2 = {
name : "The imitation game",
print : function (){
console.log(this.name)
}
}
print
function is repeated. If there were more of thesecall
method in this case.print
method out of the objects and make it as a separate function .const printThings = function (){
console.log(this.name)
}
const movie1 = {
name : "Peanut Butter Falcon"
}
const movie2 = {
name : "The imitation game"
}
call
method on the printThings
function with reference to whichever this
value we wantprintThings.call(movie1) // output : "Peanut Butter Falcon"
printThings.call(movie2) // output : "The imitation game"
printThings.call(movie1)
tells JavaScript that we want the above function's this
to point to movie1 object and similarly for movie2.const movie1 = {
name : "Peanut Butter Falcon",
print : function (){
console.log(this.name)
}
}
const printThings = function (person){
console.log(`${person} is watching ${this.name}`)
}
const movie1 = {
name : "Peanut Butter Falcon"
}
const movie2 = {
name : "The imitation game"
}
printThings
function now has an parameter called person
.printThings.call(movie1,"James") // output : "James is watching Peanut Butter Falcon"
printThings.call(movie2,"Peter") // output : "Peter is watching The imitation game"
this
argument , Rest can be passed after it like it is done in the above example.const printprintThings = function (fname, lname){
console.log(`${fname} ${lname} is watching ${this.name}`)
}
const movie1 = {
name : "Peanut Butter Falcon"
}
const movie2 = {
name : "The imitation game"
}
printThings.call(movie1,"James","Bond") // output : "James Bond is watching Peanut Butter Falcon"
printThings.call(movie2,"Peter", "Pan") // output : "Peter Pan is watching The imitation game"
call
and apply
is that apply
method calls a function with a this
value and arguments as an array instead of passing the arguments individually like the call method.apply(thisArg,argArray)
const printThings = function (fname, lname){
console.log(`${fname} ${lname} is watching ${this.name}`)
}
const movie1 = {
name : "Peanut Butter Falcon"
}
const movie2 = {
name : "The imitation game"
}
printThings.apply(movie1,["James","Bond"]) // output : "James Bond is watching Peanut Butter Falcon"
printThings.apply(movie2,["Peter", "Pan"]) // output : "Peter Pan is watching The imitation game"
bind(thisArg, arg1, ... , argN)
this
value .let printThings = function (fname, lname){
console.log(`${fname} ${lname} is watching ${this.name}`)
}
printThings = printThings.bind(movie1,"James","Bond")
printThings()
// output : "James Bond is watching Peanut Butter Falcon"
"use strict"
this.movie = "Saving Private Ryan"
const outerFunction = function(){
const innerFunction = function (){
console.log(this.movie)
}
innerFunction()
}
outerFunction()
bind
the outerFunction and use call
on the inner function and give them the this
value"use strict"
this.movie = "Saving Private Ryan"
let outerFunction = function(){
const innerFunction = function (){
console.log(this.movie)
}
innerFunction.call(this)
//Here "this" means the outerFunction
}
outerFunction = outerFunction.bind(this)
// Here "this" means the global context
outerFunction()
//Output : "Saving Private Ryan"
Within a class
section. click here to take a look at it againclass theatre {
constructor(person,movie){
this.person = person
this.movie = movie
}
display(){
console.log(`${this.person} is watching ${this.movie}`)
}
}
const jack = new theatre("jack","inception")
const callback = jack.display
callback()
display
to the this
value of the class.class theatre {
constructor(person,movie){
this.person = person
this.movie = movie
this.display = this.display.bind(this) // Here
}
display(){
console.log(`${this.person} is watching ${this.movie}`)
}
}
const jack = new theatre("jack","inception")
const callback = jack.display
callback()
setTimeout(jack.display.bind(jack),1000)
//output : jack is watching inception
const callback = jack.display.bind(jack)
callback()
//output : jack is watching inception
this
context, it borrows the context from the parent element or the context it was defined in."use strict"
this.movie = "Saving Private Ryan"
const outerFunction = () => {
const innerFunction = () => {
console.log(this.movie)
}
innerFunction()
}
outerFunction()
//Output : "Saving Private Ryan"
onClick
, we pass the method we want to execute as a callback to the handler function.this
is lost .