-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_objects.js
71 lines (59 loc) · 1.46 KB
/
07_objects.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Day-07 (19/07/20204) @iamnikhilranjan
//1.Object creation and Access
const book = {
title: 'Atomic Habits',
author: 'James Clear',
Year: 2015
};
console.log(book);
console.log(`Title: ${book.title}, Author: ${book.author}`);
//2.Objects Methods
book.printDetatils = function(){
console.log(`Title: ${book.title}, Author: ${book.author}`);
}
book.printDetatils();
console.log(book);
book.updateYear = function(year) {
this.Year = year;
};
book.updateYear(2019);
console.log(book);
//3. Nested Objects
const library = {
name: 'City library',
books: [
{
title: 'Subtle Art',
Author: 'Mark Manson'
},
{
title: "Atomic habits",
Author: 'James Clear'
}
]
}
console.log(library);
console.log("Library Name: ",library.name);
console.log("Book titles: ");
library.books.forEach(book => {
console.log(book.title);
});
book.aboutBook = function(){
console.log(`Titlle: ${this.title}, Year: ${this.Year}`);
}
book.aboutBook();
library.books.forEach((book, index) => {
console.log(`Book ${index + 1}:`);
for (let property in book){
if(book.hasOwnProperty(property)){
console.log(`${property}: ${book[property]}`);
}
}
console.log('---');
});
library.books.forEach((book, index) => {
console.log(`Book ${index+1}:`);
console.log("keys:", Object.keys(book));
console.log("values:",Object.values(book));
console.log("---");
})