【发布时间】:2023-02-14 22:25:15
【问题描述】:
将任意函数的执行速度减慢 5 秒的装饰函数。
function someFunction(a, b) {
console.log(a + b);
}
function slower(func, seconds) {
// decorator code
}
let slowedSomeFunction = slower(someFunction, 5);
slowedSomeFunction()
// will output to the console "You will get you result in 5 seconds"
//...in 5 seconds will display the result of 'someFunction*'
尝试不成功,代码中有错误
function someFunction(x) {
alert(x);
}
function slower(func, seconds) {
return function() {
setTimeout(() => func.apply(this, arguments), seconds);
}
}
let slowedSomeFunction = slower(alert, 5);
slowedSomeFunction('You will get you result in 5 seconds');
【问题讨论】:
标签: javascript decorator