You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What if we wanted to pass an argument to the function?
25
+
26
+
We can re-write the below normal function to one that uses the fat arrow syntax:*/
27
+
28
+
letadd=function(a,b){
29
+
returna+b;
30
+
};
31
+
32
+
//Can now be written as:
33
+
34
+
letadd=(a,b)=>a+b;
35
+
36
+
/*Tip
37
+
In the first example we write return a + b but in the fat arrow version we just wrote a + b. That’s because in the fat arrow syntax if it is on one line, the statement gets returned automatically without the need to use the return keyword.
38
+
this
39
+
Lets imagine we have an object with a function called sayLater, like so:*/
40
+
41
+
letobj={
42
+
name: "Pankaj",
43
+
sayLater: function(){
44
+
console.log(`${this.name}`);
45
+
}
46
+
};
47
+
obj.sayLater();
48
+
49
+
//In the sayLater function this points to obj
50
+
51
+
//So console.log(${this.name}`);` prints out Pankaj.
52
+
53
+
//Now lets imagine that we log the message using the setTimeout function.
54
+
55
+
letobj={
56
+
name: "Pankaj",
57
+
sayLater: function(){
58
+
setTimeout(function(){
59
+
console.log(`${this.name}`);
60
+
},1000);
61
+
}
62
+
};
63
+
obj.sayLater();
64
+
65
+
//In the sayLater() function we call setTimeout and then log the value of this.name, which we expect to be asim.
66
+
67
+
//In fact we get undefined printed out to the console.
68
+
69
+
/*Calling context
70
+
71
+
The reason for this is that the value of this in a function depends on how the function is called.
72
+
At it’s most basic level if the function is called as obj.sayLater(), the value of this is the calling context which in this case is obj.
73
+
What is the calling context for the anonymous function we pass to setTimeout? What will this point to inside that function?*/
74
+
75
+
setTimeout(function(){
76
+
console.log(`${this.name}`);
77
+
},1000);
78
+
//The answer is it depends.
79
+
80
+
/*In the browser it’s either undefined or the global object depending on if you are running in strict mode or not.
81
+
In node it’s an internal timeout object.
82
+
83
+
In all cases however it isn’t going to be obj, so this.name is not going to return asim,
84
+
it’s going to return undefined or raise an error.
85
+
86
+
This instability of this is an incredibly common problem in javascript that has affected it since the early days.
87
+
88
+
In ES5 there are a number of methods we can use to stabilise the value of this.
89
+
One common solution is to assign this to another variable at the top, usually called self or vm, and then always use self in the function body, like so:*/
90
+
91
+
letobj={
92
+
name: "Pankaj",
93
+
sayLater: function(){
94
+
letself=this;// Assign to self
95
+
console.log(self);
96
+
setTimeout(function(){
97
+
console.log(`${self.name}`);// Use self not this
98
+
},1000);
99
+
}
100
+
};
101
+
/*But in ES6 we can do better, if we use fat arrow functions the value of this inside a fat arrow function will be the same as the value of this outside the fat arrow function.
102
+
103
+
It uses the value of this from the surrounding code for its context. i.e. whatever this points to in the surrounding code, this will point to in the function body of the fat arrow function.
104
+
105
+
We can re-write our obj to use fat arrow syntax like so:*/
106
+
107
+
letobj={
108
+
name: "Pankaj",
109
+
sayLater: function(){
110
+
console.log(this);// `this` points to obj
111
+
setTimeout(()=>{
112
+
console.log(this);// `this` points to obj
113
+
console.log(`${this.name}`);// `this` points to obj
0 commit comments