-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_current_contex.js
19 lines (16 loc) · 934 Bytes
/
2_current_contex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Context/ Current Context
// What
// - When you are talking with your friend about some event that is host on MONDAY.
// - But you need to also clearly mention that which MONDAY are you talking about it was last week, current week or upcoming week.
// - This is the same for `this` keyword. It help to find the current context.
// - Suppose you have two object and both of them have same method. If you call the method how it will going to check that which object's method are you trying to access.
// - By using `this` keyword you are telling that which MONDAY are you talking about. I mean which object you are trying to access.
const user = {
username: "test user",
userId: "123",
getUserName: function () {
console.log(username); // It doesn't know which user are you talking about.
console.log(this.username); // By using this it will refer to current object.
// user.getUserName === this.username
},
};