20
loading...
This website collects cookies to deliver better user experience
this
... this
isn't bound to :this
appears within.this
appears within.this
appears within.this
itself doesn't have a value. JS interpreter binds the value of this
when its parent function gets invoked. but would you always trust the JS interpreter to bind the value of this
to the right object
automatically? ... I wouldn't.what is the value of this
here ?
show me where the parent function is invoked.
function displayName() {
console.log(this);
}
displayName(); // {}
this
inside displayName()
is either the global object
or, if in strict mode, it's undefined
.const player = {
name: "Mohamed",
displayName: function () {
console.log(this.name);
},
};
player.displayName(); // "Mohamed"
this
inside displayName()
will refer to player
which actually makes sense because you called the method displayName()
on the object player
. that invocation bound the object player
to this
inside of the method. good job JS interpreter. function Square(color) {
this.color = color;
}
const redSquare = new Square("red");
new
does behind the scenes.new
and this
, you will have to create and return the object manuallyfunction Square(color){
let obj = {}
obj.color = color
return obj
}
const redSquare = Suare("red");
new
actually creates the object for you and binds that object to this
... then return itfunction Square(color){
̶l̶e̶t̶ ̶t̶h̶i̶s̶ ̶=̶ ̶O̶b̶j̶e̶c̶t̶.̶c̶r̶e̶a̶t̶e̶(̶S̶q̶u̶a̶r̶e̶.̶p̶r̶o̶t̶o̶t̶y̶p̶e̶)̶; //{}
this.color = color;
̶r̶e̶t̶u̶r̶n̶ ̶t̶h̶i̶s̶;
}
const redSquare =new Square("red");
this
inside the function is a newly created empty object. this
const player = {
name: "Mohamed",
displayName: function () {
console.log(this.name);
},
};
const anotherPlayer = { name: "ahmed" };
player.displayName.call(anotherPlayer); // "ahmed"
player.displayName.apply(anotherPlayer)
this
inside displayName()
will refer to anotherPlayer
since the first parameter of call()
or "apply() is to explicitly set what this
refers to.bind()
method is a little bit different because it returns the same function after it sets the value of this
to whatever you wantconst player = {
name: "Mohamed",
displayName: function () {
console.log(this.name);
},
};
const anotherPlayer = { name: "ahmed" };
const displayName = player.displayName.bind(anotherPlayer);
displayName(); // "ahmed"
bind()
set the value of this
inside player.display
to anotherPlayer
and returned a whole new function.bind()
...I'll stick to call()
and apply()
.const player = {
name: "Mohamed",
displayName: function () {
console.log(this.name);
},
};
function callTwice(cb) {
cb();
cb();
}
callTwice(player.displayName);
undefined
undefined
this
will be the global object because that's how its parent function got invoked. bind()
to the rescueconst callback = player.displayName.bind(player);
callTwice(callback); // "Mohamed"
// "Mohamed"
this
is set based on how the function is called. With arrow functions, the value of this
is based on the function's surrounding context. In other words, the value of this
inside an arrow function is the same as the value of this
outside the function then apply the rules we mentioned earlier.