Declare variables programmatically?

前端 未结 3 928
醉梦人生
醉梦人生 2020-12-11 23:59

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

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


        
3条回答
  •  难免孤独
    2020-12-12 00:23

    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!

提交回复
热议问题