增加变量的名称 [英] Increment the name of variable

查看:69
本文介绍了增加变量的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想增加变量的名称。这样做的正确语法是什么?

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);

    var slider_+i = function(){
    //some code
}

dojo.addOnLoad(slider_+i);


推荐答案

为什么不使用数组?

var slider = [];

for (i=0; i<5; i++) {
    slider[i] = function(){
        //some code
    }

    dojo.addOnLoad(slider[i]);
}






或者,您可以访问它们基于它们所包含的对象。假设它们是全局变量(希望不是):


Alternatively, you could access them based on the object they are contained within. Assuming they are global variables (hopefully not):

for (i=0; i<5; i++) {
    window["slider_"+i] = function(){
        //some code
    }

    dojo.addOnLoad(window["slider_"+i]);
}

window [something] 是另一种访问名为的全局变量的方法

这篇关于增加变量的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆