1

I'm using global javascript variables to store some data after an Ajax request. It's used to mark days on a jqueryui datepicker and are stored in the form array[date] = type_of_date

this is the function i'm using to compute the array(s)

function get_date_exceptii(data, cod_calendar) {

if(typeof(window.date_zile_ore_modificate) !== 'undefined' && typeof(window.date_zile_nelucratoare) !== 'undefined') {
    window.date_zile_ore_modificate.length = 0;
    window.date_zile_nelucratoare.length = 0;
}
else {
    window.date_zile_ore_modificate = [];
    window.date_zile_nelucratoare  = [];
}

parametri = 'cod_calendar='+cod_calendar+'&data='+data;

$j.ajax({
    url: "proiecte/ajax/colectare_date_exceptii.php?sid="+Math.random(),
    type: 'POST',     
    async: false,
    data: parametri             
})
.done( function (msg) {
    arr_msg = msg.split('[sep1]');
    $j.each(arr_msg, function (index, val) {
        arr_exceptie = val.split('[sep]');
        if(arr_exceptie[1] == 'nelucratoare')
            window.date_zile_nelucratoare[arr_exceptie[0]] = arr_exceptie[2];
        else {
            window.date_zile_ore_modificate[arr_exceptie[0]] = arr_exceptie[2];
        }
    }); 
});
console.log(window.date_zile_ore_modificate);
console.log(window.date_zile_nelucratoare);
}

The two logs are for debugging, of course. The problem is the the arrays are never cleared even tho that section of code is executed. Is my syntax wrong or am I missunderstanding global variables and/or arrays here?

Bogdan
  • 1,869
  • 6
  • 24
  • 53
  • 6
    Why not `date_zile_ore_modificate = []` ? Also, I don't think I've ever seen `window.` used so much in a script for variables - are they really necessary? Do you have complicated scoping issues? – MDEV Oct 11 '13 at 14:24
  • 2
    `array.length = 0` should be fine, but I'd personally go with what @SmokeyPHP said – Reinstate Monica Cellio Oct 11 '13 at 14:26
  • [Empty an array in JavaScript...](http://stackoverflow.com/a/1232046/1823841) – palaѕн Oct 11 '13 at 14:27
  • length property should not be mutable IMO – Stphane Oct 11 '13 at 14:27
  • @SmokeyPHP I tried your suggestion and it seems to work, that's how I had it initially but the senior programmer hates it for some reason. – Bogdan Oct 11 '13 at 14:29
  • Where do you actually call `get_date_exceptii`? Your `console.log` lines will run *before* your AJAX `.done` function which might be part of why you're not seeing what you expect. – Matt Burland Oct 11 '13 at 14:30
  • The indices specified by `arr_exceptie[0]` might be *non-numeric*. Setting `length = 0` only clears numerically-named properties (per [ES5 15.4.5.2](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.5.2)), so properties with non-numeric names are not cleared. If you set `date_zile_ore_modificate['foo']`, for example, then setting `date_zile_ore_modificate.length=0` doesn't clear away the `foo` property. However, setting the variable to a brand new array obviously *does* clear all properties, because it blows away the old array object entirely. – apsillers Oct 11 '13 at 14:31
  • @Bogdan They probably don't like it because of the reasons brought up in the post linked by palash (about recreating the array instance and breaking references). Are the outcomes of the two methods different depending on different browsers? – MDEV Oct 11 '13 at 14:31
  • 1
    @MattBurland it won't run before the ajax because the ajax call is synchronous. – Bogdan Oct 11 '13 at 14:32
  • @SmokeyPHP I'm sure that's their reasoning but it's more on principle than anything since those reasons are not concerns in this case. There are no other refferences to these variables. – Bogdan Oct 11 '13 at 14:34
  • @apsillers or SmokeyPHP you've given me a solution and the source of the problem. One of you please add an answer for me to accept in case anyone else has this problem – Bogdan Oct 11 '13 at 14:36

1 Answers1

4

The indices specified by arr_exceptie[0] are likely non-numeric.

Setting length = 0 only clears numerically-named properties (per ES5 15.4.5.2), so properties with non-numeric names are not cleared. For example, if you set date_zile_ore_modificate['foo'] and date_zile_ore_modificate[5], then setting date_zile_ore_modificate.length=0 clears away the 5 property, but does not clear away the foo property.

However, setting the variable to a brand new array obviously does clear all properties, because it eliminates the old array object entirely. Thus, just use

window.date_zile_ore_modificate = [];
window.date_zile_nelucratoare  = [];

in all cases and eliminate you initial conditional.

Also, if your properties names are entirely non-numeric, you should probably use plain objects instead of arrays (since arrays are really only appropriate for numerically-indexed data):

window.date_zile_ore_modificate = {};
window.date_zile_nelucratoare  = {};
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • Okay there's only one problem with this. On IE whenever I try to `window.date_zile_ore_modificate = [];` I get an "undefined" error. – Bogdan Oct 14 '13 at 06:45
  • Edit, nevermind. The undefined error came from me trying to use console.log on IE with developer tools deactivated. – Bogdan Oct 14 '13 at 07:04