Just wanted to add a couple of things to Dmitry's answer. There are at least 2 differences between variables and properties in the global scope:
1) Property references are deletable but by default variables are not. window.foo = 42; delete foo; //true foo; //undefined var foo = 42; delete foo; //false foo; //42 This is because by default the internal [[Configurable]] property of variables is set to false (in ES5 terms) or the [[DontDelete]] property is true (in ES3). (Don't believe what firebug tells you - its doing an eval which assigns different defaults when creating internal properties) 2) Property definitions allow more liberal identifiers: window[42] = 42; window[42]; //42 var 42 = 42; //SyntaxError: missing variable name Check out the excellent articles Dmitry cited, plus these two articles: http://perfectionkills.com/understanding-delete/ http://javascriptweblog.wordpress.com/2010/08/09/variables-vs-properties-in-javascript/ Angus On Jan 7, 1:49 am, Dmitry Soshnikov <dmitry.soshni...@gmail.com> wrote: > On Fri, Jan 7, 2011 at 10:46 AM, Mathias Bynens <math...@qiwi.be> wrote: > > > Let’s assume you’re working in the global scope already and you want > > to create a new global variable. What do you guys recommend? Should > > the `var` keyword be used (first example) even though it’s not > > necessary in this case? Are there any other implications I should be > > aware of? What is considered to be the best practice here? > > While answers above have already clarified the case (yes, a variable in JS > is declared with using a `var` keyword), I add some notes below. > > First, if to nit-pick to technical part, strictly speaking, a variable is > defined only with using `var` keyword (there is a special section in my old > article:http://dmitrysoshnikov.com/ecmascript/chapter-2-variable-object/#abou... > ). > > However, if the technical part is not so essential, you may use an > assignment to an undeclared identifier (your case with foo = 42) for > creating a property of the global object. The only thing you need in ES3 for > that is to watch attentively and manage carefully all your variables (to > avoid collisions with other variables). > > In strict mode of ES5 though, you won't be able to use `foo = 42` case, > since such assignments are banned and cause a `ReferenceError`. > Info:http://dmitrysoshnikov.com/ecmascript/es5-chapter-2-strict-mode/#assi... > > So if you need to define a global property in the some other context, use > `global.foo = 42` (where `global` refers to the global object). Or via > `Object.defineProperty(...)`. > > P.S.: > > The main thing is just in distinguishing local and non-local variables > (there are no other ideological reasons). For example, from Python > viewpoint, this JS's `var` is a syntactic noise. What for to repeat every > time this `var` if I want to declare a variable in the local scope? Of > course `one-var` pattern with using comma may help to manage the case and to > decrease this noise. > > So this is all about which strategy to choose for distinguishing. Since > Python declares vars without a special keyword, it needs vice-versa keyword > then to define a non-local variable: > > # global var (which is > # local in the global context) > > a = 10 > > def foo(): > a = 20 # local > print(a) # 20 > > print(a) # 10 > > def bar(): > > a = 30 # local > > def baz(): > > # if we need to use `a` > # from the outer context we > # we can't use just `a = 40` > # since it will create a local `a` > # so we use a special keyword > > nonlocal a = 40 > > b = 50 > > print(a + b) # 90 > > bar() # call the inner function > > print(a) # see that `a` of bar is changed > > Other languages also differently distinguish local and non-local vars. E.g. > Lua uses a similar to JS notation -- assignment to an undeclared identifier > creates always a global var. And is we need a local var, we use `local` > keyword. > > a = 10 -- global > > function foo() > b = 20 -- global > local c = 30 -- local > end > > CoffeScript also doesn't use any keyword for declaring a variable. But in > Coffee it's not possible to create a local variable with the same name if > already there is a global variable with the same name. > > a = 10 # global > > foo = () -> > a = 20 # assignment to global `a` > b = 30 # local > > Dmitry. -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com