Skip to content

Commit 28dafa7

Browse files
committed
use of static keyword
1 parent 4f9ba24 commit 28dafa7

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

class.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Book {
1111

1212
getAge(){
1313
const years = new Date().getFullYear() - this.year;
14-
return `${this.title} is ${years} old`;
14+
return `${this.title} is ${years} years old`;
1515
}
1616

1717
revise(newYear) {
@@ -24,8 +24,8 @@ class Book {
2424

2525
const book1 = new Book('Book One', 'John Doe', '2013');
2626
console.log(book1);
27-
book1.revise('2018');
28-
console.log(book1);
29-
27+
// book1.revise('2018');
28+
// console.log(book1);
29+
console.log(book1.getAge());
3030

3131

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
</head>
99
<body>
1010
<h2>JavaScript OOP</h2>
11-
<script src="./SubClass.js"></script>
11+
<script src="./staticInClass.js"></script>
1212
</body>
1313
</html>

staticInClass.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
class Book {
3+
4+
static newBooks = true;
5+
static oldBooks = true;
6+
7+
constructor(title, author, year) {
8+
this.title = title;
9+
this.author = author;
10+
this.year = year;
11+
}
12+
13+
getSummary(){
14+
return `${this.title} was written by ${this.author} in year ${this.year}`;
15+
}
16+
17+
getAge(){
18+
const years = new Date().getFullYear() - this.year;
19+
return `${this.title} is ${years} old`;
20+
}
21+
22+
}
23+
24+
const book1 = new Book('Book One', 'John Doe', '2013');
25+
26+
// instance of Book do not contain static methods
27+
console.log(book1);
28+
29+
// static methods can only be accessed by the class
30+
console.log(Book.newBooks);
31+
console.log(Book.oldBooks);

0 commit comments

Comments
 (0)