what is differences between var a = b = 2 and var a = 2; var b=2;

时间:2015-06-26 09:33:22

标签: javascript variables global-variables

when i declare variable in a function, i face a problem.

var b = 44;
function test(){
    var a = b = 2;
}

But this works fine:

 var b = 44;
 function test(){
    var a;
    var b = 2;
 }

Variable b over rides the global b variable.

I cannot find any documentation about this behavior.

Is there any documentation about it ?

demo : http://jsfiddle.net/uq4nxk1k/1/

5 个答案:

答案 0 :(得分:3)

I don't know where you can find a documentation, but here's the explanation about the result you obtained :

Local > Global

When you declare a global variable, it's available anywhere in your file. Inside "test()", when you write :

var a = b = 2;

you are creating a new variable a, that take the value of the global variable b and changing, at the same time, the value of b to 2 -> you are overriding his value.

When you write (inside test()) :

var a, b; 

or

var a; 
var b;

you're declaring two more variables, that are only known inside your function and, as local > global, if you write b = 2, you can face two situations :

  1. if you console.log(b) inside test(), you obtain 2
  2. if you console.log(b) outside test(), you obtain 44

Declaration != Assignment

Very very important ->

  • var a, b; is a declaration

  • a = b = 25; is an assignment (I'd say double assignment)

  • var a = b = 25 is a declaration and an assignment at the same time.

I hope it helps!!!:) Just tell me if something is unclear or if you need any other explanation.

答案 1 :(得分:1)

var in a function scope doesn't override the variables declared in outer scope. Let me explain what your test() function does:

// these variables are global:
var a = "out";
var b = "out";
var c = "out";
var d = "out";
var e = "out";
var f = "out";
var g = "out";


function test() {
    // the following line is equal to
    // var a; var b; var c = "in";
    var a, b, c = "in"; 
    // the local variable b gets a value of "in"
    b = "in";
    // the following means:
    // declare local d which references global e which references global f
    // and assign "in" to them; which is why only global e and global f are changed to "in";
   // *edit more like: f = "in"; e = f; var d = e;
    var d = e = f = "in";
    // declare local variable g and assign "in" to it
    var g = "in";
}
test();
// here back in the global scope a, b, c, d and g were not changed
// so a == "out", b == "out", c == "out", d == "out" and g == "out"
// but e == "in" and f == "in" because you've changed them from within test()

答案 2 :(得分:1)

Just to note

When you declare with primitive values:

var a = b = 2;

Equivalent to:

var b = 2;
var a = b;

As you realize, both a and b are assigned with the same value.

However, when you assign object instead of primitive values:

var a = b = [1,2,3,4];

This is also equivalent to:

var b = [1,2,3,4];
var a = b;

Which means both a and b share the same reference. So any changes you made on b will affect a, and vice versa:

a.push(5); 
// a <--- [1,2,3,4,5] 
// b <--- [1,2,3,4,5] !! be ware of this! b will also get this effect

Keep in mind: When you use a shortand a = b = c = value. All variables will be assigned with the same value. But in case of object assignment, all variables will share the same reference which refers to a value. Anytime you use this, always be aware of this effect.

Therefore, for object assignment, this definition:

var a = b = [1,2,3,4]; // change a WILL affect b

is not producing exactly the same effect as

var a = [1,2,3,4]; var b = [1,2,3,4]; // change a won't affect b

答案 3 :(得分:0)

I think it's because you're declaring variables inside the function that won't be recognised by $(".resultg").text("g = " + g);

But in var d = e = f = "in"; you're declaring a new local variable "d" that is equal to global variables e and f, and then assign "in" to all of them. As e and f are global variables, only in this case "in" reassignment will be recognised by $(".resultg").text("g = " + g);

答案 4 :(得分:0)

The question is not strange also the answer is not strange it is simple. The thing is just global variable concept

// the below a,b,c,d,e,f,g are global variable

var a = "out";
var b = "out";
var c = "out";
var d = "out";
var e = "out";
var f = "out";
var g = "out";

so now a,b,c,d,e,f,g are assigned with a value 'out'

so if you are yrying to alert anyone it will result out

// your function starts here

    function test() {
        var a, b, c = "in"; // Here a,b,c are local variables and never overright the global variable values 
    so this statement is meaning less when we try to print values from outside this function 

        b = "in";//here is the trick happends and confusion begins actually the b is our local variable created in the test function it is not global 
so when we change the value of b here it will not affect the global one 

var d = e = f = "in"; // the same thing happend here to here e,f are global and d is local // so if we try to print the global variable values we got 
    a=out
    b=out
    c=out
    d=out
    e=in
    f=in 
    g=out

        var g = "in";

    then here also it is a local variable so result will be 
    a=out
    b=out
    c=out
    d=out
    e=in
    f=in 
    g=out

    }

    test();

    $(".resulta").text("a = " + a);
    $(".resultb").text("b = " + b);
    $(".resultc").text("c = " + c);
    $(".resultd").text("d = " + d);
    $(".resulte").text("e = " + e);
    $(".resultf").text("f = " + f);
    $(".resultg").text("g = " + g);