Question

Because I'm used to C and Java, I've found Javascript's lack of block scope a little irksome. Sometimes I find myself wanting to declare and then immediately execute an inline function just to overcome this issue. For example:

... 

if (x == 0) { 
  (function () {
    var i;

    for (i = 0; i < 10; i++) {
      ...
    }
  })();
}

...

Otherwise, I feel the need to declare all of the variables for a function at the top of the scope, in order to avoid forgetting about the lack of block scope. But having a huge var statement at the beginning of a function looks clumsy to me.

The way I've shown above feels wasteful, but I don't have any idea of what it costs to declare inline functions in the first place. Is doing it this way a bad idea? Is there a better way to solve my qualm with Javascript's scoping?

Was it helpful?

Solution

What's wrong with writing it as follows:

if (x === 0) { // you should always use === over == in JavaScript
    for (var i = 0; i < 10; i++) {
        // body
    }
}

Just because JavaScript doesn't support block scoping doesn't mean that you can't declare variables in block scopes.

You can declare variables anywhere. However they'll be visible everywhere in the scope of the function.

The only problem caused by the lack of block scoping in JavaScript is when you define a function inside a loop which accesses free variables (the infamous loop problem).

Read the following StackOverflow thread to wrap your head around it: JavaScript closure inside loops – simple practical example

JavaScript is simple. Way more simple than C or Java. It's a lot more sensible than Java too. Java is a pathetic language.

If you want the JavaScript community to be daggers against you then you're doing a wonderful job. Otherwise just embrace JavaScript. In the words of Douglas Crockford:

In my own practice, I have found that working with JavaScript has made me a better Java programmer because it introduced me to a useful set of dynamic techniques.

Source: A Survey of the JavaScript Programming Language

OTHER TIPS

JavaScript scoping, short of ES5 features, is at the function level. It's like how C used to require you to declare variables before anything else; you just have to live with it.

Your code is not syntactically correct. You've got a statement that starts with the function keyword, but no function name; it's also followed by (). I assumed you're talking about a function declaration statement. If you're just instantiating a function as part of an expression, then scope doesn't really figure into that, at least external to the function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top