Context: this is a task in a javascript tutorial. The code is supposed to generate a subclass ExtendedClock
of Clock
that allows the console to show the current time in a customized interval of time.
Class Clock {
constructor({ template }) {
this._template = template;
}
_render() {
let date = new Date();
let hours = date.getHours();
if (hours < 10) hours = '0' + hours;
let mins = date.getMinutes();
if (mins < 10) min = '0' + mins;
let secs = date.getSeconds();
if (secs < 10) secs = '0' + secs;
let output = this._template
.replace('h', hours)
.replace('m', mins)
.replace('s', secs);
console.log(output);
}
stop() {
clearInterval(this._timer);
}
start() {
this._render();
this._timer = setInterval(() => this._render(), 1000);
}
}
Here comes the weird line in solution given in the tutorial:
class ExtendedClock extends Clock {
constructor(options) {
super(options);
let { precision=1000 } = options; // what is this?
this._precision = precision;
}
start() {
this._render();
this._timer = setInterval(() => this._render(), this._precision);
} }; Another problem: My code is the following one. Why doesn't this work?
class ExtendedClock extends Clock {
constructor({template, precision = 1000}) {
super({template})
this._precision = precision
}
start() {
super.start()
this._timer = setInterval(() => this._render(), this._precision)
}
}