Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61b9d13

Browse files
committed
Merge branch 'prep-class23' of github.com:HackYourFuture/JavaScript3 into prep-class23
2 parents b03fbaa + 045aaf3 commit 61b9d13

18 files changed

+610
-1
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
"tabindex",
1414
"whiteframe"
1515
]
16-
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Object Literal
2+
const book1 = {
3+
title: 'Book One',
4+
author: 'John Doe',
5+
year: 2013,
6+
getSummary: function() {
7+
return `${this.title} was written by ${this.author} in ${this.year}.`;
8+
},
9+
};
10+
11+
const book2 = {
12+
title: 'Book Two',
13+
author: 'Jane Doe',
14+
year: 2016,
15+
getSummary: function() {
16+
return `${this.title} was written by ${this.author} in ${this.year}.`;
17+
},
18+
};
19+
20+
console.log(book1.getSummary());
21+
console.log(book2.getSummary());
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Constructor
2+
function Book(title, author, year) {
3+
this.title = title;
4+
this.author = author;
5+
this.year = year;
6+
this.getSummary = function() {
7+
return `${this.title} was written by ${this.author} in ${this.year}.`;
8+
};
9+
}
10+
11+
// Instantiate an Object
12+
const book1 = new Book('Book One', 'John Doe', 2013);
13+
const book2 = new Book('Book Two', 'Jane Doe', 2016);
14+
15+
console.log(book1.getSummary());
16+
console.log(book2.getSummary());
17+
18+
console.log(book1);
19+
20+
book1.turnPage();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Constructor
2+
function Book(title, author, year) {
3+
this.title = title;
4+
this.author = author;
5+
this.year = year;
6+
}
7+
8+
Book.prototype.getSummary = function() {
9+
return `${this.title} was written by ${this.author} in ${this.year}.`;
10+
};
11+
12+
Book.prototype.getAge = function() {
13+
const years = new Date().getFullYear() - this.year;
14+
return `${this.title} is ${years} years old.`;
15+
};
16+
17+
// Revise / Change Year
18+
Book.prototype.revise = function(newYear) {
19+
this.year = newYear;
20+
this.revised = true;
21+
};
22+
23+
// Instantiate an Object
24+
const book1 = new Book('Book One', 'John Doe', 2013);
25+
const book2 = new Book('Book Two', 'Jane Doe', 2016);
26+
27+
console.log(book1.getSummary());
28+
console.log(book2.getSummary());
29+
30+
console.log(book2);
31+
book2.revise(2018);
32+
console.log(book2);
33+
34+
console.log(book2.getAge());
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Book Constructor
2+
function Book(title, author, year) {
3+
this.title = title;
4+
this.author = author;
5+
this.year = year;
6+
}
7+
8+
Book.prototype.getSummary = function() {
9+
return `${this.title} was written by ${this.author} in ${this.year}.`;
10+
};
11+
12+
Book.prototype.getAge = function() {
13+
const years = new Date().getFullYear() - this.year;
14+
return `${this.title} is ${years} years old.`;
15+
};
16+
17+
// Magazine Constructor
18+
function Magazine(title, author, year, month) {
19+
Book.call(this, title, author, year);
20+
this.month = month;
21+
}
22+
23+
// Inherit Prototype
24+
Magazine.prototype = Object.create(Book.prototype);
25+
26+
// Use Magazine Constructor
27+
Magazine.prototype.constructor = Magazine;
28+
29+
Magazine.prototype.updateMonth = function(month) {
30+
this.month = month;
31+
};
32+
33+
// Instantiate Magazine Object
34+
const mag1 = new Magazine('Mag One', 'John Doe', 2018, 'Jan');
35+
36+
console.log(mag1);
37+
console.log(mag1.getSummary());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Object of Protos
2+
const bookProtos = {
3+
getSummary: function() {
4+
return `${this.title} was written by ${this.author} in ${this.year}.`;
5+
},
6+
getAge: function() {
7+
const years = new Date().getFullYear() - this.year;
8+
return `${this.title} is ${years} years old.`;
9+
},
10+
};
11+
12+
// Create Object
13+
const book1 = Object.create(bookProtos);
14+
book1.title = 'Book One';
15+
book1.author = 'John Doe';
16+
book1.year = 2013;
17+
18+
const book2 = Object.create(bookProtos, {
19+
title: { value: 'Book Two' },
20+
author: { value: 'Jane Doe' },
21+
year: { value: 2016 },
22+
});
23+
24+
console.log(book1);
25+
console.log(book2);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Book {
2+
constructor(title, author, year) {
3+
this.title = title;
4+
this.author = author;
5+
this.year = year;
6+
}
7+
8+
getSummary() {
9+
return `${this.title} was written by ${this.author} in ${this.year}.`;
10+
}
11+
12+
getAge() {
13+
const years = new Date().getFullYear() - this.year;
14+
return `${this.title} is ${years} years old.`;
15+
}
16+
17+
revise(newYear) {
18+
this.year = newYear;
19+
this.revised = true;
20+
}
21+
22+
static topBookStore() {
23+
return 'Barnes & Noble';
24+
}
25+
}
26+
27+
// Instantiate an Object
28+
const book1 = new Book('Book One', 'John Doe', 2013);
29+
const book2 = new Book('Book Two', 'Jane Doe', 2016);
30+
31+
console.log(book1);
32+
33+
console.log(book1.getSummary());
34+
console.log(book2.getSummary());
35+
36+
console.log(book2);
37+
book2.revise(2018);
38+
console.log(book2);
39+
40+
console.log(book2.getAge());
41+
42+
console.log(Book.topBookStore());
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Book {
2+
constructor(title, author, year) {
3+
this.title = title;
4+
this.author = author;
5+
this.year = year;
6+
}
7+
8+
getSummary() {
9+
return `${this.title} was written by ${this.author} in ${this.year}.`;
10+
}
11+
}
12+
13+
class Magazine extends Book {
14+
constructor(title, author, year, month) {
15+
super(title, author, year);
16+
this.month = month;
17+
}
18+
19+
updateMonth(month) {
20+
this.month = month;
21+
}
22+
}
23+
24+
// Instantiate Magazine Object
25+
const mag1 = new Magazine('Mag One', 'John Doe', 2018, 'Jan');
26+
27+
console.log(mag1);
28+
console.log(mag1.getSummary());

0 commit comments

Comments
 (0)