-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path51-methods.js
More file actions
41 lines (34 loc) · 970 Bytes
/
Copy path51-methods.js
File metadata and controls
41 lines (34 loc) · 970 Bytes
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
// Methods - functions inside objects
/* Basic Example
const person = {
firstName: "Vaishnao",
age: 21,
about: function () {
// console.log(`My name is ${firstName} and my age is ${age}`); //this will give error
console.log(`My name is ${this.firstName} and my age is ${this.age}`); // this represents the object calling the function.
},
};
person.about();
*/
/* Example to understand this keyword better */
function personInfo() {
console.log(`My name is ${this.firstName} and my age is ${this.age}`);
}
const person1 = {
firstName: "Vaishnao",
age: 21,
about: personInfo,
};
const person2 = {
firstName: "Vaishnavi",
age: 22,
about: personInfo,
};
const person3 = {
firstName: "Arpit",
age: 21,
about: personInfo,
};
person1.about(); // As person1 calls the function this will represent the person1 and print its properties
person2.about(); // Here this represents person2
person3.about(); // Here this represents person3