-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththisoperator.js
More file actions
52 lines (37 loc) · 1.03 KB
/
thisoperator.js
File metadata and controls
52 lines (37 loc) · 1.03 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
//In JavaScript, the this keyword refers to an object.
//Which object depends on how this is being invoked (used or called)
const student = {
firstName: "VIVEK",
lastName : "POTLA",
id : 6699,
fullName () {
return this.firstName + " " + this.lastName;
}
};
//here this operator points to the student object (locally)
console.log(student.fullName())
//The Function call() Method
const person1 = {
fullName(){
return this.firstName + " " + this.lastName;
}
}
const person2 = {
firstName:"Vivek",
lastName: "Potla",
}
// Return "Vivek Potla":
person1.fullName.call(person2);
//With the bind() method, an object can borrow a method from another object.
const human = {
firstName:"Bear",
lastName: "Grylls",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
const member = {
firstName:"Vivek",
lastName: "Potla",
}
let fullName = human.fullName.bind(member); // human is borrowing the member object