Javascript 有人能解释一下这个看似奇怪的赋值`{key=value}}=argument吗`
上下文:这是一项任务。代码应该生成Javascript 有人能解释一下这个看似奇怪的赋值`{key=value}}=argument吗`,javascript,class,subclass,Javascript,Class,Subclass,上下文:这是一项任务。代码应该生成Clock的子类ExtendedClock,该子类允许控制台以自定义的时间间隔显示当前时间 Class Clock { constructor({ template }) { this._template = template; } _render() { let date = new Date(); let hours = date.getHours(); if (hours < 10) hours = '
Clock
的子类ExtendedClock
,该子类允许控制台以自定义的时间间隔显示当前时间
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);
}
}
}
};
另一个问题:我的代码如下。为什么这样不行
class ExtendedClock extends Clock {
constructor({template, precision = 1000}) {
super({template})
this._precision = precision
}
start() {
super.start()
this._timer = setInterval(() => this._render(), this._precision)
}
}
如果有默认值的属性,那么这个奇怪的代码只是覆盖默认值的一种简便方法
let options = { precision: 999, template: 'h:m:s' };
let { precision = 1000 } = options; // precision === 999
let options = { template: 'h:m:s' };
let { precision = 1000 } = options; // precision === 1000
对于第二个问题,我需要更多的投入。您会遇到哪个错误?如果有默认值的属性,这段奇怪的代码只是覆盖默认值的一种简便方法
let options = { precision: 999, template: 'h:m:s' };
let { precision = 1000 } = options; // precision === 999
let options = { template: 'h:m:s' };
let { precision = 1000 } = options; // precision === 1000
对于第二个问题,我需要更多的投入。您会遇到哪个错误?这是具有默认值的对象解构语法。如果要分解的对象包含
precision
键,则将使用该值,但如果不包含该值,则将使用1000
如果密钥存在,将使用其值:
const options = { precision: 800 };
const { precision = 1000 } = options;
console.log(precision); // logs 800
但如果密钥不存在,将使用默认值:
const options = {};
const { precision = 1000 } = options;
console.log(precision); // logs 1000
您的代码可能无法工作,因为当您调用
super.start()
时,超类会以
setInterval(() => this._render(), 1000);
调用后,代码会启动一个循环,但两个循环现在都在运行,导致超类的setInterval
每隔1000毫秒调用一次render函数,然后子类的循环每隔precision
ms单独调用一次render函数
不要在循环开始时调用
super.start()
,试着只调用this.\u render()
这是具有默认值的对象解构语法。如果要分解的对象包含precision
键,则将使用该值,但如果不包含该值,则将使用1000
如果密钥存在,将使用其值:
const options = { precision: 800 };
const { precision = 1000 } = options;
console.log(precision); // logs 800
但如果密钥不存在,将使用默认值:
const options = {};
const { precision = 1000 } = options;
console.log(precision); // logs 1000
您的代码可能无法工作,因为当您调用
super.start()
时,超类会以
setInterval(() => this._render(), 1000);
调用后,代码会启动一个循环,但两个循环现在都在运行,导致超类的setInterval
每隔1000毫秒调用一次render函数,然后子类的循环每隔precision
ms单独调用一次render函数
不要在循环开始时调用
super.start()
,试着只调用this.\u render()
这行奇怪的代码在JavaScript中正在分解结构,这意味着选项将是一个js对象,如果有键(精度)然后它的值将被分配给precision变量,否则precision将等于1000我猜您的解决方案不会像预期的那样工作,因为它启动了两个计时器。这一奇怪的行在JavaScript中正在分解结构,这意味着选项将是一个js对象,如果有键(precision)然后它的值将被分配给精度变量,否则精度将等于1000我想你的解决方案不会像预期的那样工作,因为它会启动两个计时器。谢谢!我没有收到任何错误消息,但是行this.\u timer=setInterval(()=>this.\u render(),this.\u precision)
没有执行,因此时钟不会每10000毫秒打印一次。相反,它仍然每1000毫秒显示一次。谢谢!我没有收到任何错误消息,但是行this.\u timer=setInterval(()=>this.\u render(),this.\u precision)
没有执行,因此时钟不会每10000毫秒打印一次。相反,它仍然每1000毫秒显示一次。