闭包怪异问题: 为啥这个闭包函数不缓存 count 值?

function createIncrement() {let count = 0;function increment() {count++;}let message = Count is ${count};function log() {console.log(message);}

return [increment, log];}const [increment, log] = createIncrement();increment();increment();increment();log(); // What is logged?

问题

为啥 log()输出 Count is 0?increment 闭包 ha 函数为啥不缓存 count 的值?

为啥把 let message = Count is ${count}移到 log 函数内就能输出 Count is 3?function log() {let message = Count is ${count};console.log(message);}

2022-11-14 17:36

Click to rate this post!
[Total: 0 Average: 0]

相关文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注