forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_prototypes.js
More file actions
34 lines (27 loc) · 792 Bytes
/
3_prototypes.js
File metadata and controls
34 lines (27 loc) · 792 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
// Constructor
function Book(title, author, year) {
this.title = title;
this.author = author;
this.year = year;
}
Book.prototype.getSummary = function() {
return `${this.title} was written by ${this.author} in ${this.year}.`;
};
Book.prototype.getAge = function() {
const years = new Date().getFullYear() - this.year;
return `${this.title} is ${years} years old.`;
};
// Revise / Change Year
Book.prototype.revise = function(newYear) {
this.year = newYear;
this.revised = true;
};
// Instantiate an Object
const book1 = new Book('Book One', 'John Doe', 2013);
const book2 = new Book('Book Two', 'Jane Doe', 2016);
console.log(book1.getSummary());
console.log(book2.getSummary());
console.log(book2);
book2.revise(2018);
console.log(book2);
console.log(book2.getAge());