File tree Expand file tree Collapse file tree 7 files changed +43
-63
lines changed
1-js/09-classes/03-class-inheritance/2-clock-class-extended Expand file tree Collapse file tree 7 files changed +43
-63
lines changed Original file line number Diff line number Diff line change 1+ [ js src="solution.view/extended-clock.js"]
Original file line number Diff line number Diff line change 11class Clock {
22 constructor ( { template } ) {
3- this . _template = template ;
3+ this . template = template ;
44 }
55
6- _render ( ) {
6+ render ( ) {
77 let date = new Date ( ) ;
88
99 let hours = date . getHours ( ) ;
@@ -15,7 +15,7 @@ class Clock {
1515 let secs = date . getSeconds ( ) ;
1616 if ( secs < 10 ) secs = '0' + secs ;
1717
18- let output = this . _template
18+ let output = this . template
1919 . replace ( 'h' , hours )
2020 . replace ( 'm' , mins )
2121 . replace ( 's' , secs ) ;
@@ -24,11 +24,11 @@ class Clock {
2424 }
2525
2626 stop ( ) {
27- clearInterval ( this . _timer ) ;
27+ clearInterval ( this . timer ) ;
2828 }
2929
3030 start ( ) {
31- this . _render ( ) ;
32- this . _timer = setInterval ( ( ) => this . _render ( ) , 1000 ) ;
31+ this . render ( ) ;
32+ this . timer = setInterval ( ( ) => this . render ( ) , 1000 ) ;
3333 }
3434}
Original file line number Diff line number Diff line change @@ -2,11 +2,11 @@ class ExtendedClock extends Clock {
22 constructor ( options ) {
33 super ( options ) ;
44 let { precision= 1000 } = options ;
5- this . _precision = precision ;
5+ this . precision = precision ;
66 }
77
88 start ( ) {
9- this . _render ( ) ;
10- this . _timer = setInterval ( ( ) => this . _render ( ) , this . _precision ) ;
9+ this . render ( ) ;
10+ this . timer = setInterval ( ( ) => this . render ( ) , this . precision ) ;
1111 }
1212} ;
Original file line number Diff line number Diff line change 11<!DOCTYPE HTML>
2- < html >
2+ < script src ="clock.js "> </ script >
3+ < script src ="extended-clock.js "> </ script >
34
4- < head >
5- < title > Console clock</ title >
6- </ head >
5+ < script >
6+ let lowResolutionClock = new ExtendedClock ( {
7+ template : 'h:m:s' ,
8+ precision : 10000
9+ } ) ;
710
8- < body >
9-
10- < script src ="clock.js "> </ script >
11- < script src ="extended-clock.js "> </ script >
12-
13- < script >
14- let lowResolutionClock = new ExtendedClock ( {
15- template : 'h:m:s' ,
16- precision : 10000
17- } ) ;
18-
19- lowResolutionClock . start ( ) ;
20- </ script >
21-
22- </ body >
23- </ html >
11+ lowResolutionClock . start ( ) ;
12+ </ script >
Original file line number Diff line number Diff line change 11class Clock {
22 constructor ( { template } ) {
3- this . _template = template ;
3+ this . template = template ;
44 }
55
6- _render ( ) {
6+ render ( ) {
77 let date = new Date ( ) ;
88
99 let hours = date . getHours ( ) ;
@@ -15,7 +15,7 @@ class Clock {
1515 let secs = date . getSeconds ( ) ;
1616 if ( secs < 10 ) secs = '0' + secs ;
1717
18- let output = this . _template
18+ let output = this . template
1919 . replace ( 'h' , hours )
2020 . replace ( 'm' , mins )
2121 . replace ( 's' , secs ) ;
@@ -24,11 +24,11 @@ class Clock {
2424 }
2525
2626 stop ( ) {
27- clearInterval ( this . _timer ) ;
27+ clearInterval ( this . timer ) ;
2828 }
2929
3030 start ( ) {
31- this . _render ( ) ;
32- this . _timer = setInterval ( ( ) => this . _render ( ) , 1000 ) ;
31+ this . render ( ) ;
32+ this . timer = setInterval ( ( ) => this . render ( ) , 1000 ) ;
3333 }
3434}
Original file line number Diff line number Diff line change 11<!DOCTYPE HTML>
2- < html >
2+ < script src ="clock.js "> </ script >
3+ < script >
4+ let clock = new Clock ( {
5+ template : 'h:m:s'
6+ } ) ;
7+ clock . start ( ) ;
38
4- < head >
5- < title > Console clock</ title >
6- </ head >
79
8- < body >
10+ /* Your class should work like this: */
911
10- <!-- source clock -->
11- < script src ="clock.js "> </ script >
12- < script >
13- let clock = new Clock ( {
14- template : 'h:m:s'
15- } ) ;
16- clock . start ( ) ;
17-
18-
19- /* Your class should work like this: */
20-
21- /*
12+ /*
2213
23- let lowResolutionClock = new ExtendedClock({
24- template: 'h:m:s',
25- precision: 10000
26- });
27-
28- lowResolutionClock.start();
29- */
30- </ script >
31-
32- </ body >
14+ let lowResolutionClock = new ExtendedClock({
15+ template: 'h:m:s',
16+ precision: 10000
17+ });
3318
34- </ html >
19+ lowResolutionClock.start();
20+ */
21+ </ script >
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ importance: 5
66
77We've got a ` Clock ` class. As of now, it prints the time every second.
88
9+
10+ [ js src="source.view/clock.js"]
11+
912Create a new class ` ExtendedClock ` that inherits from ` Clock ` and adds the parameter ` precision ` -- the number of ` ms ` between "ticks". Should be ` 1000 ` (1 second) by default.
1013
1114- Your code should be in the file ` extended-clock.js `
You can’t perform that action at this time.
0 commit comments