【发布时间】:2018-05-01 07:55:27
【问题描述】:
我正在阅读一个 JS 教程,并遇到了这个例子:
function makeCounter() {
function counter() {
return counter.count++;
};
counter.count = 0;
return counter;
}
let counter = makeCounter();
counter.count = 10;
console.log(counter()) // logs 10
我不明白为什么它没有记录 11 而不是 10?如果我们已经将count 属性设置为10 并存储了递增count 属性的function counter,为什么该属性没有递增到11?
【问题讨论】:
-
counter.count打印11 -
counter()将记录10,但counter.count将记录11。++增量发生在返回值之后。如果你想要它,你可以写return ++counter.count。
标签: javascript