2

For some reason I cannot assign anything to a global array without using .push().

(function() {
    var globalEmail = [];

    var testClear = function() {
        var arr = [1, 2, 3];

        if (globalEmail.length > 1) {
            globalEmail = [];
        }
        else {
            globalEmail = arr;
        }
    };

    window.globalEmail = globalEmail;
    window.testClear = testClear;
})();

If I call testClear() and then globalEmail in the console, globalEmail remains unchanged.

kLabz
  • 1,837
  • 1
  • 14
  • 14
j.doe
  • 1,214
  • 2
  • 12
  • 28
  • 3
    Works fine for me... pasted your code in the console, called `testClear();`, then `window.globalEmail` - result: `(3) [1, 2, 3]` `globalEmail` is also populated. – tymeJV Jan 05 '18 at 21:44
  • 2
    You are loosing your references when you do `globalEmail = [];` or `globalEmail = arr;`, so window.globalEmail is different than the new globalEmail array. You need to modify the array in place (with splice and concat, for your example). – kLabz Jan 05 '18 at 21:46
  • @tymeJV: try executing this code inside an IIFE, and then use `globalEmail` and `testClear` to reproduce the issue. – kLabz Jan 05 '18 at 21:48
  • the code works fine! If the code has to be executed in a IIFE, this should be mentioned in the question – Marouen Mhiri Jan 05 '18 at 21:49
  • @kLabz Thank you, this solution works fine. If you want to add it as an answer I will accept it. :) – j.doe Jan 05 '18 at 21:53
  • I agree that it's not clear, but this code is run in another context as when @j-doe is running `testClear` and checking `globalEmail`, that's why there is `window.globalEmail` and `window.testClear`. You run both in the same context, so you have a direct reference to `globalEmail`, that's why it works. – kLabz Jan 05 '18 at 21:54

3 Answers3

2

For empty an array, you could set the lenght to zero and for assigning, you could use Array#push with spread syntax ...

var globalEmail = [],
    testClear = function() {
        var arr = [1, 2, 3];

        if (globalEmail.length > 1) {
            globalEmail.length = 0;
        } else {
            globalEmail.push(...arr);
        }
    };

testClear();
console.log(globalEmail);

testClear();
console.log(globalEmail);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

As said in comments, you are loosing references here because you assign globalEmail to new arrays, so window.globalEmail is different than the new globalEmail array.

You need to modify the array in place (with splice and concat, for your example).

kLabz
  • 1,837
  • 1
  • 14
  • 14
0

You want to define the globalEmail variable as global, not local.

(function() {
    window.globalEmail = []; // this goes here

    var testClear = function() {
        var arr = [1, 2, 3];

        if (window.globalEmail.length > 1) {
            window.globalEmail = [];
        }
        else {
            window.globalEmail = arr;
        }
    };

    //window.globalEmail = globalEmail; <-- not here
    window.testClear = testClear;
})();

The window. is not absolutely required, but the var globalEmail; within a function defines a local variable, which is not what you wanted in the first place.

Although, if the point of testClear() is to clear any array, then it is not properly implemented. Instead it should take a parameter and you work on that parameter.

function testClear(a)
{
    var arr = [1, 2, 3];

    if(a.length > 1) {
        a.splice(0);
    }
    else {
        a.splice(0);
        a.push.apply(a, arr);
    }
}

testClear(window.globalEmail);

Then the testClear() function makes more sense, I think.

Note that there are drawbacks with a.push.apply() which breaks on large arrays. See here How to extend an existing JavaScript array with another array, without creating a new array? for details.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156