-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththis-Keyword.js
54 lines (44 loc) · 1.18 KB
/
this-Keyword.js
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
const ninja = {
name: "Uzumaki",
jutsu: ["Shadow clone", "Rasengan"],
toString: function () {
//console.log(`${this.name} has jutsus like ${this.jutsu}`);
this.jutsu.forEach((powers) => {
//arrow function works best with "this keyword"
//to bind to the actual object of this
console.log(`${this.name} has ${powers}`);
});
},
};
ninja.toString();
console.log("---------===---------");
var person = {
firstName: "Rakib",
lastName: "Talukder",
skills: ["HTML", "CSS", "JavaScript", "Tailwind", "SASS"],
fullInfo: function () {
console.log(`Candidate name is ${this.firstName} ${this.lastName}`);
console.log(`Skills: ${this.skills}`);
//print each skill
// this.skills.forEach(function (items) {
// console.log(` skill: ${items} `);
// });
},
};
person.fullInfo();
console.log("---------------------");
function hi(arg) {
console.log(this + " hello to " + arg);
}
hi("Readers");
hi.call("thisValue", "Readers");
let obj = {
name: "Sebu Elias",
hiAgain: function (arg) {
console.log(this.name + " welcomes " + arg);
},
};
// So when we call
obj.hiAgain("reader");
// It is called like,
obj.hiAgain.call(obj, "reader");