Defining JavaScript variables…

Joseph Pachod

Thursday, 3rd of September 2009 at 10:05:54 PM
hi
Do you know the difference in JavaScript between these variables declarations :
- myVar = “foo”
- var myVarVar = “foo”
In fact, the first one will have an unlimited scope, and thus accessible from the outer world, whereas the second one will only be accessible at this level and below.
As such, this is why the line “$.fn.myFunction = function(){bla}; ” is used to declare new function to the jQuery object.
Similarly, make sure you use “var” in front of your own JavaScript variables to avoid name collisions !
Hope it helps !
++
PS : funny how I’ve plenty of long posts in mind but not yet written (out of laziness/lack of time) while at the same time I’m eager to write small ones on stuff I’ve just discovered… May be I should consider twitter more closely !
PPS : the (french) source of this article
http://danielhagnoul.developpez.com/...?page=sommaire
Partager