Javascript 我应该如何理解;array.forEach(函数)";在所附的代码中?

Javascript 我应该如何理解;array.forEach(函数)";在所附的代码中?,javascript,Javascript,++符号在这里做什么? 我知道这是一个人为的例子,它是一个array.forEach(function()) 应用但我似乎不太明白这一点。请带我看一下这个代码。为什么输出是3和16?这个[arg]引用了“类”计数器的属性this.count是一个计数器,它表示this.sum增加了多少倍++只是将某个值增加1 换句话说,如果某个变量foo为0,foo++与foo=foo+1相同 基本上是这样的 您在每个值上传递了[2,5,9],并且forEach循环 第一次迭代: this.sum=this.s

++符号在这里做什么? 我知道这是一个人为的例子,它是一个array.forEach(function())
应用但我似乎不太明白这一点。请带我看一下这个代码。为什么输出是3和16?这个[arg]引用了“类”
计数器的属性
this.count
是一个计数器,它表示this.sum
增加了多少倍<代码>++只是将某个值增加1

换句话说,如果某个变量
foo
0
foo++
foo=foo+1
相同

基本上是这样的

您在每个值上传递了
[2,5,9]
,并且
forEach
循环

第一次迭代:

this.sum=this.sum+2/=>2

this.count=this.count+1/=>1

第二次迭代:

this.sum=this.sum+5/=>2+5=7

this.count=this.count+1/=>1+1=2

最后一次迭代:

this.sum=this.sum+7/=>7+9=16

this.count=this.count+1/=>2+1=3


因此,得到16和3。

此[arg]
引用了“类”
计数器的属性
this.count
是一个计数器,它表示this.sum
增加了多少倍<代码>++只是将某个值增加1

换句话说,如果某个变量
foo
0
foo++
foo=foo+1
相同

基本上是这样的

您在每个值上传递了
[2,5,9]
,并且
forEach
循环

第一次迭代:

this.sum=this.sum+2/=>2

this.count=this.count+1/=>1

第二次迭代:

this.sum=this.sum+5/=>2+5=7

this.count=this.count+1/=>1+1=2

最后一次迭代:

this.sum=this.sum+7/=>7+9=16

this.count=this.count+1/=>2+1=3


因此,得到16和3。创建一个
计数器
对象:

function Counter()
    { this.sum = 0;
    this.count = 0;
     }

Counter.prototype.add = function(array){
array.forEach(function(entry){
this.sum+= entry;
++this.count;
},this);
};

var obj = new Counter();
obj.add([2,5,9]);
console.log(obj.count);
// 3
console.log(obj.sum);
//16
调用
计数器
对象的
add()
函数,参数为数字为
2
5
9
的数组:

var obj = new Counter();
关于
add()
函数:

obj.add([2,5,9]); 
对于数组的每个元素,
forEach()

  • 通过对数组的每个元素求和,增加
    sum
    number变量:

    this.sum+=条目

  • 调用
    forEach()
    后,立即增加
    count
    number变量:

    ++this.count

++this.count是一个预增量运算符。这意味着计数在此语句中是递增的

在这种情况下,它不会改变任何内容,因为在同一语句中不使用递增的结果。 您可以使用
this.count++

例如,如果在同一语句中递增并在控制台中输出此新值,则有意义:

Counter.prototype.add = function(array){
  function(array){
    array.forEach(function(entry){
      this.sum+= entry;
      ++this.count;
     },this);
  };
使用pre-increment,新值将仅反映在下一条语句中:

var count = 0;
console.log(++this.count); // display 1

创建一个
计数器
对象:

function Counter()
    { this.sum = 0;
    this.count = 0;
     }

Counter.prototype.add = function(array){
array.forEach(function(entry){
this.sum+= entry;
++this.count;
},this);
};

var obj = new Counter();
obj.add([2,5,9]);
console.log(obj.count);
// 3
console.log(obj.sum);
//16
调用
计数器
对象的
add()
函数,参数为数字为
2
5
9
的数组:

var obj = new Counter();
关于
add()
函数:

obj.add([2,5,9]); 
对于数组的每个元素,
forEach()

  • 通过对数组的每个元素求和,增加
    sum
    number变量:

    this.sum+=条目

  • 调用
    forEach()
    后,立即增加
    count
    number变量:

    ++this.count

++this.count是一个预增量运算符。这意味着计数在此语句中是递增的

在这种情况下,它不会改变任何内容,因为在同一语句中不使用递增的结果。 您可以使用
this.count++

例如,如果在同一语句中递增并在控制台中输出此新值,则有意义:

Counter.prototype.add = function(array){
  function(array){
    array.forEach(function(entry){
      this.sum+= entry;
      ++this.count;
     },this);
  };
使用pre-increment,新值将仅反映在下一条语句中:

var count = 0;
console.log(++this.count); // display 1

请带我看一下这个代码。为什么它的输出是3和16-最好的一个,以帮助您在这里是在Chrome/Firefox的javascript调试器。打开developer tools/F12并尝试一下。这里的++this.count相当于this.count=this.count+1请带我看一下这段代码。为什么它的输出是3和16-最好的一个,以帮助您在这里是在Chrome/Firefox的javascript调试器。打开developer tools/F12并尝试一下。这里的++this.count相当于this.count=this.count+1