Javascript 每次添加数字a";对于{}";语句被称为

Javascript 每次添加数字a";对于{}";语句被称为,javascript,math,Javascript,Math,我在函数中有一个for语句,如下所示: for(i=0;i<=nc;i++){ var nd = //how many times this for statement has run+1 //if the for statement runs for the first time nd would be 1, second time nd would be 2, the for statement will run a total of "nc" times this.id=nd }

我在函数中有一个for语句,如下所示:

for(i=0;i<=nc;i++){
var nd = //how many times this for statement has run+1
//if the for statement runs for the first time nd would be 1, second time nd would be 2, the for statement will run a total of "nc" times
this.id=nd }

(i=0;i你的意思是这样的吗

for(i=0;i<=nc;i++){
   var nd = i+1;
   this.id=nd;
}

for(i=0;i在函数外部声明nd

var nd = 0;
function...
    nd++;
    this.id = nd;

我不知道你想用它实现什么

for(i=0;i<=nc;i++)
{
   this.id = i+1; 
}

for(i=0;i你是说它运行了多少次?如果你想知道迭代的次数,只需使用
i
。在这种情况下,为什么不直接执行
this.id=i+1
?在每次迭代中创建一个新变量来存储它似乎是一种浪费(除非循环中大量使用了of cource nd)@galaxyAbstractor如果他想多次使用它,他不必每次都执行
i+1
,它只是
i
的一个副本,创建了不必要的变量,效果很好。将在3分钟内接受此答案,谢谢!