Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reserved Word Behavior [duplicate]

While creating a small counter based game, I had an array like this:

var status = ["day","dusk","night","dawn"];

If I tried to access the first index of the array, I would get:

console.log(status[0]); //yields "d"

@monners mentioned it might be a reserved word, so I changed the variable name to xstatus and it worked fine.

My question is: why would status[0] return only the first letter of the first index?

like image 655
Sterling Archer Avatar asked Nov 19 '13 04:11

Sterling Archer


People also ask

What are examples of reserved words?

The following are more examples of reserved words. abstract , if , private , this , double , implements , throw , boolean , else , import , public , throws , break , return , byte , extends , int , short , true , false , case , interface , static , try , catch , final , long , void . Edit Me on GitHub!

Are reserved words and keywords same?

Keywords have a special meaning in a language, and are part of the syntax. Reserved words are words that cannot be used as identifiers (variables, functions, etc.), because they are reserved by the language. In practice most keywords are reserved words and vice versa.

Can reserved words be redefined?

Further, reserved words may not be redefined by the programmer, but predefineds can often be overridden in some capacity. Languages vary as to what is provided as a keyword and what is a predefined. Some languages, for instance, provide keywords for input/output operations whereas in others these are library routines.

How do you escape a reserved word in SQL?

To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').


2 Answers

You're modifying window.status which cannot be set to an array:

https://developer.mozilla.org/en-US/docs/Web/API/Window.status


There is some unexplained behaviour in Firefox. While both status and var status at the global scope provide references to the window.status property, var status doesn't flatten the array:

status = ["meagar"];
console.log(window.status[0]); // 'm'

vs

var status = ["meagar"];
console.log(window.status[0]); // 'meagar'
like image 188
meagar Avatar answered Oct 19 '22 17:10

meagar


Because it will be saving your array as a flat string and d is the first character (position 0) of the string.


I believe this goes years back into the old Navigator statusbar days (remember those ticker status bars). The status could only output as string --- arrays, when set to string, are flattened and comma-delimited (e.g. var ar=['foo','bar']; alert(ar);)

like image 45
vol7ron Avatar answered Oct 19 '22 19:10

vol7ron