-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23_Test_Keyword.js
More file actions
76 lines (46 loc) · 1.32 KB
/
23_Test_Keyword.js
File metadata and controls
76 lines (46 loc) · 1.32 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 1}
const user ={
username : "Tarun",
age : 22,
welcomeMessage : function(abc) {
console.log(`${abc.username} , welcome to my world`)
}
}
user.welcomeMessage(user)
user.username = "Alferd"
user.welcomeMessage(user)
// 2} // "this" is used to excess the username inside the function without taking it as a parameter.
const info ={
username : "Tarun",
age : 22,
welcomeMessage : function() {
console.log(`${this.username} , welcome to my world`)
}
}
info.welcomeMessage()
info.username = "Sam"
info.welcomeMessage()
// 3} // "this" will be holding the object only in which it is defined. without taking it is as parameter
const info2 ={
username : "Tarun",
age : 22,
welcomeMessage : function() {
console.log(`${this.username} , welcome to my world`)
console.log(this)
}
}
info2.welcomeMessage()
// NOTE ::::
// 1}
console.log(this) // gives {} as value in the node environment but "window object" in browser.
// 2}
function chai() {
console.log(this) // gives many values in the node environment when this is printed inside a function .
}
chai()
// 3}
function chaiWithMe() {
let username = "tarun"
console.log(this.username) // undefined :: this type of syntax only works inside the objects and not in the functions.
}
chaiWithMe()