2008-11-30 23:25JavaScript global variablesI admit, scope can be a tough concept to get your head around, especially when different programs do it differently, and JavaScript is no exception. In fact, JavaScript makes quite a few design decisions which, while logical, can go unnoticed even by programmers with a fair amount of experience in the language. This blog post is not the place to list and explain all of them, but I will try to give some insight into the global variable issue, and how persistence of variables works. Hopefully this knowledge can be useful to people making web pages which involve storing a lot of state, like JavaScript games require. Example pageLet’s start with a simple HTML page with a script built into it: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript"> var count=0; function changeText(htmlElement) { count += 1; textElement = htmlElement.firstChild; htmlElement.removeChild(textElement); newTextElement = document.createTextNode("Change count: "+count); htmlElement.appendChild(newTextElement); } </script> </head> <body> <a onclick="changeText(this);" style="font-weight:bold;background-color:#ccc;color:#00c">Original text</a> </body> </html> Ignoring the JavaScript code for the moment, you should find that when you load that page in your browser, you are able to change the text of the link (which doesn’t actually go anywhere) by repeatedly clicking on it. You will also notice, if you check with the W3C validator, that this is a valid XHTML document. What you should already knowThe script in this example page makes use of the proper functions for manipulating the XHTML document, which may not be familiar to people, so I will explain roughly what is going on here. Firstly, the function has a reference to the HTML element which was clicked, because the Anyway, with a reference to the link that was clicked, the code proceeds to get a reference to the text inside it, namely “Original text”. It doesn’t extract the string itself, but simply stores a reference to the part of the document which consists of that string. This reference is assigned to a variable called “textElement”. Then, the The scope bitThe important question, then, is how does the variable count work? Well, the first thing to notice is that it is defined at the beginning of the document, before the function. This makes it global and accessible to the function, without the function having to specifically pull it into its scope. This means that the function can make changes to it by assigning values to it (but not using Is there anything left to say? If someone was writing code that takes keyboard input from the user, they’d know that JavaScript treats common inputs as strings, right? What about the tricks for turning strings into numbers? Trackbacks
Trackback specific URI for this entry
No Trackbacks
|
QuicksearchCategoriesSyndicate This BlogBlog Administration |