-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
40 lines (32 loc) · 1.03 KB
/
objects.js
File metadata and controls
40 lines (32 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
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
gender: "male",
occupation: "Engineer",
hobbies: ["reading", "traveling", "photography"],
// get name
getFullName: function () {
return `${this.firstName} ${this.lastName}`;
},
// check is he adult
isAdult: function () {
return this.age >= 18;
},
// adding hobbies
addHobby: function (hobby) {
return this.hobbies.push(hobby);
},
}
// Accessing object properties
console.log(person.firstName); // Output: "John"
console.log(person.age); // Output: 30
// Accessing object methods
console.log(person.getFullName()); // Output: "John Doe"
console.log(person.isAdult()); // Output: true
// Modifying object properties
person.lastName = "Smith";
console.log(person.getFullName()); // Output: "John Smith"
// Using a method to add a new hobby
person.addHobby("painting");
console.log(person.hobbies); // Output: ["reading", "traveling", "photography", "painting"]