1.JS怎么定义一个常量对象,内部属性无法修改
let obj = {
name: 'xf',
age: 22
}
Object.defineProperty(obj, 'age', {
writable: false
})
Object.defineProperties(obj, {
name: {
writable: false
},
age: {
writable: false
}
})
obj.age = 23;
console.log(obj);
2.实现一个repeat()方法:
function` `repeat (func, times, wait) { ... ``
const repeatFunc = repeat(console.log, 4, 3000)
repeatFunc(``"helloworld"``)``
我的解决方案
function` `repeat (func, times, wait) { ... ``
return function(str) {
let repeat = times;
let timer = setInterval(() => {
func(str);
repeat--;
if(repeat == 0) {
clearInterval(timer);
}
}, wait);
}
const repeatFunc = repeat(console.log, 4, 3000)
repeatFunc(``"helloworld"``)``