我在 Javascript 中有一组全局计数器变量:
var counter_0 = 0;
var counter_1 = 0;
var counter_2 = 0;
等等
然后我有一个 Javascript 函数,它接受一个映射到这些全局计数器的“索引”号。在这个函数中,我需要使用传递给函数的“索引”值来读取和写入这些全局计数器。
我希望它如何工作的示例,但当然根本不起作用:
function process(index) {
// do some processing
// if 'index' == 0, then this would be incrementing the counter_0 global variable
++counter_+index;
if (counter_+index == 13)
{
// do other stuff
}
}
我希望我想要完成的事情很清楚。如果不是,我会尽力澄清。谢谢。
编辑澄清:
我不是要增加计数器的名称,而是要增加计数器包含的值。