无法弄清楚为什么我无法打印计数(Can't figure out why I can't print the count)
function createCounter(countt) {
var count = countt
return {
increment: function() {
count = count + 1
//return console.log(count)
},
currentValue: function() {
return console.log(count)
}
}
}
var counterStartingAt5 = createCounter(5)
var counterStartingAtMinus2 = createCounter(-2)
为什么我不能打印console.log(count)? 如果我在增量或currentValue上使用console.log它不起作用,它只是不打印它。 它应该可以访问计数,但由于某种原因它不会返回值...任何人都可以解释它吗?
function createCounter(countt) { var count = countt return { increment: function() { count = count + 1 //return console.log(count) }, currentValue: function() { return console.log(count) } } } var counterStartingAt5 = createCounter(5) var counterStartingAtMinus2 = createCounter(-2)Why can't I print the console.log(count)? It doesn't work if I use console.log on the increment or on the currentValue, it just don't print it. It should have access to the count, but for some reason it doesn't return the value... can anyone please explain it?
最满意答案
在调用currentValue函数之前,它不会打印任何内容。 请参阅以下工作示例:
function createCounter(countt) { var count = countt return { increment: function() { count = count + 1 //return console.log(count) }, currentValue: function() { return console.log(count) } } } var counterStartingAt5 = createCounter(5); counterStartingAt5.currentValue(); counterStartingAt5.increment(); counterStartingAt5.currentValue();It won't print anything until you call the currentValue function. See the following working example:
function createCounter(countt) { var count = countt return { increment: function() { count = count + 1 //return console.log(count) }, currentValue: function() { return console.log(count) } } } var counterStartingAt5 = createCounter(5); counterStartingAt5.currentValue(); counterStartingAt5.increment(); counterStartingAt5.currentValue();
更多推荐
发布评论