so I encountered weird problem here. I have an array status=new Array(). Then I iterate from 0 to N-1, and assign status[i]="idle"; I tried to do alert to check the values, and they are all assigned to a character coma ,. Anyone knows what's wrong?
var status=new Array();
window.onload = function() {
for(var i=0;i<5;i++) {
status[i]="idle";
alert(status[i]);
}
}
1 Answer 1
Use a different variable name (or better yet, don't use global variables at all). There's a window.status property already, and apparently something isn't letting you shadow it with your own (which surprises me slightly; I wonder if the array is being coerced to a string on assignment or something). At global scope, var creates properties on the window object, which is why window.status is relevant.
This example (source) replicates your problem (for me, using Chrome), and this example (source) with just a changed name shows the correct series of alerts.
Note that this is browser-specific. On Firefox, even your old code shows me the correct series of alerts. E.g., Firefox is allowing us to redefine window.status, but Chrome isn't.
4 Comments
var status in your fiddle isn't creating a global variable. If you do a "view frame source" on the result pane, you'll see what I mean.