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/#about-variables
).

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/#assignment-to-an-undeclared-identifier

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

Reply via email to