-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
58 lines (46 loc) · 1.3 KB
/
this.js
File metadata and controls
58 lines (46 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// this keyword always refer to something like object
// this refers to object on how function is invoked
// intially this keyword resembles to the window object
// Implicity binding
console.log(this);
let user = {
name: "sudhakar",
age: 24,
getName() {
console.log(this.name); // here this always refers to imediate parent in case of normal functions
},
};
const address = {
name: "sudhakar",
city: "guntur",
town: {
townName: "vijayawada",
getDetails() {
console.log(this.name + this.townName); // see here at present this referes to town ka object
//as town object doesnot have name it returns undefined
},
},
};
// In normal function this always refers to immediate parent object
const arrow = {
name: "sudhakar",
age: 24,
getName: () => {
console.log(this.name); // here In arrow function this always refers to parent function not immediate object
// as there is no parent fucntion so it tends to global and global does not have name so undefined
},
};
const arowAddress = {
name: "sudhakar",
city: "guntur",
getDetails() {
arrowGetDetails = () => {
console.log(this.name);
};
arrowGetDetails(); // see here it references to parent function
},
};
user.getName();
address.town.getDetails();
arrow.getName();
arowAddress.getDetails();