Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't populate array called `status`

Tags:

javascript

I am trying to do something really simple - initialize an array in Javascript. And it's not working in Google Chrome. Here is the code:

status = [];
for(i=0; i < 8; i++)
  status[i]=false;

alert(status.length); //It says 0 when it should say 8

What gives?

like image 747
tinkerr Avatar asked Jun 21 '10 06:06

tinkerr


3 Answers

The assignment of your status variable, clashes with the window.status property.

Chrome simply refuses to make the assignment.

The window.status property, sets or gets the text in the status bar at the bottom of the browser.

I would recommend you to either, rename your variable or use an anonymous function to create a new scope, also remember to always use var for declaring variables:

(function () {
  var status = [];

  for (var i = 0; i < 8; i++)
    status[i] = false;

  alert(status.length);
})();
like image 114
Christian C. Salvadó Avatar answered Oct 22 '22 04:10

Christian C. Salvadó


Change the variable name. Seems like status is a property of window, and Chrome makes it inmutable . I didn't expect that, too.

like image 21
Chubas Avatar answered Oct 22 '22 03:10

Chubas


The problem here is what status is attached to. You are using it off the global/window scope.

Back in the good ole days we were able to set the text in the status bar. How you would do it is by setting window.status to a string value. So what you are doing is NOT setting a variable, but changing the string of the browser's status bar.

like image 29
epascarello Avatar answered Oct 22 '22 02:10

epascarello