最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Declare variables programmatically? - Stack Overflow

programmeradmin4浏览0评论

I am trying to create a bunch of variables like this:

function l(){
    var a1 = 2,
        a2 = 4,
        a3 = 6,
        a4 = 8,
          .
          .
        a20 = 40;
}

But this is taking too many lines, and I am searching for a way to do it smarter. This is what I have come up:

function l(){
    for(var i=0; i<20; i++){
        var ("a"+i) = 2*i;
    }
}

But it probably won't work, and if it works (it does not) the variables will still be inside the for scope. Any ideas?

window["a"+i] or eval(...)

These don't work because I don't want them to be in the global scope.

Usually an Array would be fine, but I am just experimenting if this is possible in JavaScript. Maybe in the future I would encounter something like this.

I am trying to create a bunch of variables like this:

function l(){
    var a1 = 2,
        a2 = 4,
        a3 = 6,
        a4 = 8,
          .
          .
        a20 = 40;
}

But this is taking too many lines, and I am searching for a way to do it smarter. This is what I have come up:

function l(){
    for(var i=0; i<20; i++){
        var ("a"+i) = 2*i;
    }
}

But it probably won't work, and if it works (it does not) the variables will still be inside the for scope. Any ideas?

window["a"+i] or eval(...)

These don't work because I don't want them to be in the global scope.

Usually an Array would be fine, but I am just experimenting if this is possible in JavaScript. Maybe in the future I would encounter something like this.

Share Improve this question edited Dec 9, 2012 at 7:04 Derek 朕會功夫 asked Dec 9, 2012 at 6:52 Derek 朕會功夫Derek 朕會功夫 94.3k45 gold badges195 silver badges253 bronze badges 4
  • 1 Just a note: they will not be in the for scope--everything in JavaScript is function scoped. – JoshRagem Commented Dec 9, 2012 at 6:54
  • 1 i would suggest using an array – Justin McDonald Commented Dec 9, 2012 at 6:55
  • If you really don't want to use an array for whatever reason, and you dont want to use window because of global scope, just replace window with some other var... – Krease Commented Dec 9, 2012 at 6:58
  • 1 @Derek they are only scoped within the for statement if you use let instead of var. But let is part of ES6. It's in Firefox now, though. But Josh is right, they are function scoped, not block-scoped because you used var. – Ray Toal Commented Dec 9, 2012 at 6:59
Add a comment  | 

3 Answers 3

Reset to default 16

Don't do this. Do. Not. Do. This. Use an array.


Given the trouble you're having creating them programmatically, how do you think you'd refer to them programmatically?

I guess its better for you to go with an array, like:

function l(){
    var a = [];
    for(var i=0; i<20; i++){
        a[i] = 2*i;
    }
}

Or if you really want the long list of variables, try this. But its using eval()

function l(){
    var js = '';
    for(var i=0; i<20; i++){
        js += 'var a'+i+' = '+2*i+';'
    }
    eval (js);
}

As a matter of fact, I think using a object is a good idea.

var scope = {}
for (var i=1;i<=20;i++) {
  scope['a'+i] = 'stuff';   
}

The result will be you have a scope object that contain every newly create variable you want!

发布评论

评论列表(0)

  1. 暂无评论