-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththisKeyword.js
More file actions
104 lines (71 loc) · 2.6 KB
/
thisKeyword.js
File metadata and controls
104 lines (71 loc) · 2.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Q - 1 ) variable vs Property
// explation => obj.greet() is a methods invocations,
// that's why this keyword inside the method equal object
const obj = {
msg: "Hello, COSMOS!",
greet() {
const msg = "Hello, WORLD!"
return this.msg // print property values if we remove this keyword then print variable in the function
}
}
let ans = obj.greet()
console.log(ans)
// -----------------------------------------------------------------------------------------
// Q - 2 ) PLANET Name ?
// explation => When a function invoked as a constructor new Planet("EARTH !")
// this keyword inside the constructor function equals the constructed object
function Planet(name) {
this.name = name
this.getPlanetName = () => this.name
}
const names = new Planet("EARTH !")
console.log(names.getPlanetName())
let {getPlanetName} = names
console.log(getPlanetName())
// -----------------------------------------------------------------------------------------
// Q - 3 ) Delayed Greetings ?
// explation => while setTimeOut() function uses the timeObj.getMessage() as
// a callback, still, it invokes timeObj.getMessage() as a Regular function,
// rather then method
let timeObj = {
msg: "Hello Time !",
getMessage() {
console.log(this.msg)
}
}
setTimeout(timeObj.getMessage, 3000)
// -----------------------------------------------------------------------------------------
// Q - 4 ) Artificial method ?
// explation => there are at least 3 ways how to loggedMessage() as a Method on the object
let artObj = {
msg: "Hello AI !"
}
function loggedMessage() {
console.log(this.msg)
}
//using call() method
loggedMessage.call(artObj)
// using apply() method
loggedMessage.apply(artObj)
//using bind() method it's create a Bound function
let bindAns = loggedMessage.bind(artObj)
bindAns()
// -----------------------------------------------------------------------------------------
// Q - 5 ) Greeting and farewell ?
// explation => When calling greetObj.greet(), inside the method greet() this value equals object.
// because greet is a regular function. Thus greetObj.greet() returns 'Hello, COSMOS!'.
// But arroGreet() is an arrow function, so this value inside of an arrow function
// always equals this of the outer scope. The outer scope of farewell() is the global
// scope, where this is the global object. Thus greetObj.arroGreet() actually returns
// 'HELLO, ${window.who}!', which evaluates to 'HELLO, undefined!'.
let greetObj = {
who: "COSMOS!",
greet() {
return `HELLO ${this.who}`
},
arroGreet: () => {
return `HELLO ${this.who}`
}
}
console.log(greetObj.greet())
console.log(greetObj.arroGreet())