1

It's been very useful for writing functions that require iterating through variables, but I've heard users advising against using similar methods.

Is there something wrong with writing code this way?

Example of use:

Say I have three arrays and want to add a value to all three:

array1=[];
array2=[1,2];
array3=[4,4];

for (var i=1;i< 4; i++){
    window['array'+i].push(1);
}
John Fred
  • 77
  • 7
  • 1
    "functions that require iterating through variables" - what kind of functions are you writing that require iterating through all global variables? – user2357112 Oct 03 '16 at 00:03
  • 1
    I would advise you to use at least an object (`var obj = {};`) to store the variables you want to iterate through instead of the window to avoid collision or unexpected behaviors. – Emile Bergeron Oct 03 '16 at 00:03
  • 1
    Also, please show us how you're using this, so we can judge the real case. – Emile Bergeron Oct 03 '16 at 00:04
  • Unless you are writing something like a debugger or some other development tool, it is extremely uncommon that one needs to "iterate through variables". – Thilo Oct 03 '16 at 00:04
  • adding example now – John Fred Oct 03 '16 at 00:04
  • 2
    It's not iterating through variables that's wrong, it's more using the global window object. Also note window is browser only, node.js for example does not have this object. – Keith Oct 03 '16 at 00:06

2 Answers2

1

If you're going to need to iterate through a set of variables, plan ahead and put them inside an object:

var myArrays = {
    array1: [],
    array2: [1, 2],
    array3: [4, 4],
    "any-name-you-want-works": []
};

Accessing them is still straightforward:

myArrays.array1.push(1);
myArrays["any-name-you-want-works"].push(1);

Adding a new array to myArrays:

myArrays.array4 = [3, 5];

And iterating is made easy (this is the recommended way):

for (var arr in myArrays) {
    if (object.hasOwnProperty(arr)) {
        arr.push(1);
    }
}

If you don't need to support old browsers, you can use newer features as well (source):

Object.keys(myArrays).forEach(function(key, index) {
    myArrays[key].push(1); 
});

More info on Object.keys().

Then, if you're using a popular lib like underscore or lodash, you can do lots of thing easily on objects and arrays:

_.chain(myArrays)
    .each(function(arr) { arr.push(1) }) // adds one to each array
    .first() // takes the first array
    .map(function(val) { return val *2; }) // and multiply each value
    .reverse() // then reverse those values
    .value(); // and get the array

As I mentioned in a comment, you shouldn't pollute the global window object and also iterate through it to avoid collision or unexpected behaviors.

Wrapping your code inside an IIFE (Immediatly-Invoked Function Expression) is always a good idea, instead of using the global scope, for multiple reasons, but that's a whole other discussion and we can achieve what you want without it (for this question anyway).

Community
  • 1
  • 1
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
0

It's not good practice to stick all of your variables in the global scope. There are certain cases where it's acceptable, for instance if you have a library that you want to be available everywhere, but it's generally best to avoid using window, particularly in the way you are using it. The good news is you don't need window to do what you want to at all. You can just add your arrays as attributes on an object, and reference them using strings the same way you did with window (which is just a globally scoped object). Note the use of var to scope the object locally.

var arrays = {
    array1: [],
    array2: [1,2],
    array3: [4,4]
};

for (var i=1;i< 4; i++){
    arrays['array'+i].push(1);
}
flimsy
  • 204
  • 1
  • 10