无法弄清楚为什么我无法打印计数(Can't figure out why I can't print the count)

编程入门 行业动态 更新时间:2024-10-25 00:27:37
无法弄清楚为什么我无法打印计数(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(); 
  
 

更多推荐

本文发布于:2023-08-07 20:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弄清楚   figure   count   print

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!