admin管理员组

文章数量:1310067

I've been told that javascript variables should all e before they are used in a function, such that:

function contrived() {
  var myA, myB;

  myA = 10;
  myB = 20;

  return myA + myB;
}

Is prefered over:

function furtherContrivance() {
  var myA = 10;
  var myB = 20;

  return myA + myB;
}

Is this the case? And why is that?

I've been told that javascript variables should all e before they are used in a function, such that:

function contrived() {
  var myA, myB;

  myA = 10;
  myB = 20;

  return myA + myB;
}

Is prefered over:

function furtherContrivance() {
  var myA = 10;
  var myB = 20;

  return myA + myB;
}

Is this the case? And why is that?

Share Improve this question asked Dec 2, 2011 at 3:05 KhanzorKhanzor 5,0103 gold badges28 silver badges41 bronze badges 1
  • It's only for a reference question, you can ment to explain the collabs. – David Rodrigues Commented Dec 2, 2011 at 3:07
Add a ment  | 

6 Answers 6

Reset to default 5

I guess some people might prefer the former style because that's how it works inside. All local variables exist for the entire lifetime of the function, even if you use var to declare them in the middle of the function.

There's nothing wrong with declaring variables later in the function, syntax-wise, it might just be confusing as the variables will then exist before the line that declares them. Hence this function:

function bar() {
    alert(foo); // Alerts "undefined". Not an error because the variable does exist.
    var foo = 10;
    alert(foo); // Alerts the value 10.
}

Is equivalent to this:

function bar() {
    var foo;
    alert(foo);
    foo = 10;
    alert(foo);
}

Another related fact is that nested function definitions (done using function foo() { ... }) will get moved to the top of the containing function as well, so they will be available even if the code that calls them es before them.

There is no difference in this case between this two. I'd go with:

function furtherContrivance() {
  var myA = 10,
      myB = 20;

  return myA + myB;
}

which is knows as single var pattern in javascript.

What you really need to take care of is defining your variables in the beginning of your functions. There is a thing in javascript called variables hoisting which means that variable definitions used in function "raise" on top. It's best described by an example:

var x = 'global'; // global (bounded to a global object which is window in browsers)
function func() { 
    alert(x); // undefined (you expected 'global', right?)
    var x = 'local';
    alert(x); // local
}
func();

what really happens is called (as I said) variables hoisting (definition of x raises on top), so the code above is actually the same as:

var x = 'global';
function func() {
    var x; // definition of `x` raised on top (variables hoisting)
    alert(x); // undefined in a local scope
    x = 'local';
    alert(x);
}

What a javscript interpreter does is it looks inside a function, gathers locally defined variables and raises them on top - this might be a good reason why you should use single var pattern.

Yes, the variable declaration should e at the top of the function:

function foo() {
  var a, b;
}

However, initializing variables can be part of the declaration:

function foo() {
  var a = 10, b = 20;
}

The reasoning behind declaring all variables at the top of the function where they are used is to avoid scope confusion.

Here is an example of bad code:

function foo() {
  var b;
  for (var i = 0; i < 5; i++) {
    var a;
    a = b = i;
    setTimeout(function(){
      console.log(a, b);
    }, 1000);
  }
}

If you execute the code, it will log 4, 4 5 times, rather than counting up. This is because only functions act as closures and introduce new scope. In JavaScript, any var declaration within a function gets executed at the beginning of the function.

This makes the above error much more visible:

function foo() {
  var a, b, i;
  for (i = 0; i < 5; i++) {
    a = b = i;
    setTimeout(function(){
      console.log(a, b);
    }, 1000);
  }
}

In the example you give this is absolutely not the case. In a language like Javascript, it will be more of a developer preference, but it won't have any impact on the result.

Yes, place them at the top. It adds to code clarity.

Try this example:

var x = 1;

(function() {

    x++;
    alert( x );  // What will this alert show?

    var x = 'done';
    alert( x );

})();

Looks like it should alert 2, but it alerts NaN.

This is because the variable declaration is hoisted to the top, but the initialization stays in the same place.

So what is actually happening is:

var x = 1;

(function() {
    var x;

    x++;
    alert( x );  // What will this alert show? NaN

    x = 'done';
    alert( x );

})();

...which makes the NaN expected.

For readability, it's definitely preferred.

However, Javascript "hoists" declarations. Hoisting means that vars and functions will be automatically moved to the top of their scope. This allows you to do things such as use a function before it's declared:

function myScope()
{
   test();

   function test()
   {
      //...
   }
}

This can lead to some confusion, especially if variables within block scopes are declared. For example:

for(var i in foo)
{
   var e = myFunc();
}

The declaration of e will be hoisted to the top of the closure, and e will be initialized to undefined. This allows for some interesting non-intuitive situations, such as:

if(!foo) //Will not throw reference error because foo is declared already
{
   var foo = {};
}

So, regardless of how you declare your variables, they'll all get "moved up" to the top of the function anyway.

Hope this helps!

本文标签: Javascript variable declarations at the head of a functionStack Overflow