- In this article, we will take a deep dive into the javascript
this
and also talk about how it behaves in a different scenario. - in many programming languages
this
is the reference to the object but in the case of javascriptthis
is based on how the function is called. - Before I share the rule with you let me give an example of how javascript
this
defers when I call the function differently.
1.
function checkThis(){
console.log(this === window); // true
}
checkThis();
2.
let obj = {
myFunction(){
console.log(this === obj); // true
}
}
obj.myFunction();
- Here, I am using the object shorthand syntax. if don't know what it is please do check it out this blog post
3.
let obj = {
myFunction(){
console.log(this === obj); // false
console.log(this === window) // true
}
}
consr alias = obj.myFunction;
alias();
- Here I think every example except 2nd example gave the unexpected output so let me give the technique that will make your life easier.
- The rule of thumb is if you call the function in the form of
func()
this
will refer to thewindow
object and if call the function in the form ofobj.func()
this
will refer to thatobject
. - now let's review the above three exam. In 1st example we called
checkThis
function in the form offunc()
sothis
will bewindow
object. - In 2nd example we called the
obj.myFunction()
. it is the form ofobject.func()
sothis
will beobj
- now 3rd example is interesting in this example we assigned
obj.myFunction
to the variablealias
and we called that variable. now you may think alias is equal toobj.myFunction
sothis
will beobj
but remember the rule we called thealias()
which the form offunc()
. sothis
will be thewindow
object. - let's try another example for practice
4.
let obj = {
myFunction(){
console.log(this === obj); // true
function fn(){
console.log(this === obj); // false
console.log(this === window); // true
}
fn();
}
}
obj.myFunction();
- here when we call
obj.myFunction()
,this
will beobj
which is expected but if you callfn()
inobj.myFunction
,this
will bewindow
object becausefn()
was called infunc()
form.
this with new
keyword:
- Our rule will not apply when we call the function
new func(..)
way. - when we call the function
new func()
it creates an object andthis
points to that object .
5.
function person(name) {
this.name = name;
}
var p1 = new person("pranshu");
console.log(p1.name); // "pranshu"
Arrow function and this
:
- In ES6, javascript introduced a new type of anonymous function called
arrow function
. - arrow functions do not have
this
binding, so they getthis
value from enclosing lexical context.
6.
let obj = {
myFunction(){
console.log(this === obj); // true
const fn =()=>{
console.log(this === obj); // true
console.log(this === window); //false
}
fn();
}
}
- Above code is the same as 4. but we are using the
arrow
function forfn
instead of the traditional function. As seen in the 4th example normally,
this
infn
would bewindow
because it was called infunc()
form but herefn
isarrow
function, and arrow do not havethis
. so just like any variable; the arrow function would look forthis
in the lexical chain and it will get the parent'sthis
which isobj
.Well that is a lot of information and easily forgettable, so if you don't want to remember the rules for this and you want hard bind
this
, you can use methods likecall
,apply
andbind
which I will cover in the next post.