Increment the name of variable

前端 未结 2 1149
萌比男神i
萌比男神i 2020-12-11 12:07

Basically I want to increment the name of the variable. What is the correct syntax to do this?

for (i=0; i<5; i++) {
    eval(\"var slider_\" + i);

             


        
2条回答
  •  忘掉有多难
    2020-12-11 12:59

    The right way to do so is to use an object or array. This should work:

    var slider = {}; // object
    // var slider = [] ; // array
    for (i=0; i<5; i++) {
        slider[i] = function() {
            // some code ...
        }
        dojo.addOnLoad(slider[i]);
    }
    

提交回复
热议问题