var a=b=2和var a=2之间的区别是什么;var b=2;

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

本文关键字:var 是什么 区别 之间      更新时间:2023-09-26

当我在函数中声明变量时,我会遇到一个问题。

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

但这很好:

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

变量b凌驾于全局变量b之上。

我找不到任何关于这种行为的文档。

有关于它的文件吗?

演示:http://jsfiddle.net/uq4nxk1k/1/

我不知道在哪里可以找到文档,但下面是关于您获得的结果的解释:

本地>全局

当您声明一个全局变量时,它在文件中的任何位置都可用。在"test()"中,当你写:

var a = b = 2;

您正在创建一个新的变量a,它采用全局变量b的值,同时将b的值更改为2->您正在覆盖他的值。

当你写(内部测试()):

var a, b; 

var a; 
var b;

您正在声明另外两个变量,这两个变量只在您的函数内部已知,并且,作为local>global,如果您编写b=2,您可能会面临两种情况:

  1. 如果在test()中使用console.log(b),则获得2
  2. 如果您在test()外部使用console.log(b),您将获得44

声明!=分配

非常非常重要->

  • 变量a,b是一个声明

  • a=b=25是一个赋值(我说是双重赋值)

  • vara=b=25是一个声明,同时也是一个赋值

我希望它能有所帮助!!!:)如果有什么不清楚的地方,或者你需要其他解释,请告诉我。

函数作用域中的

var不会覆盖外部作用域中声明的变量。让我解释一下test()函数的作用:

// 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()

只需注意

使用基元值声明时:

var a = b = 2;

相当于:

var b = 2;
var a = b;

正如您所意识到的,ab都被分配了相同的值。

但是,当您指定对象而不是基元值时:

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

这也相当于:

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

这意味着ab共享相同的引用。因此,您对b所做的任何更改都会影响a,反之亦然:

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

请记住:当您使用shortanda = b = c = value时。所有变量都将被分配相同的值。但在对象赋值的情况下,所有变量都将共享引用某个值的相同引用。任何时候你使用这个,都要注意这个效果。

因此,对于对象分配,这个定义是:

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

不会产生与完全相同的效果

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

我认为这是因为您在函数中声明了$(".resultg").text("g = " + g); 无法识别的变量

但在var d = e = f = "in";中,您声明了一个新的局部变量"d",它等于全局变量ef,然后将"in"分配给所有这些变量。由于ef是全局变量,只有在这种情况下,"in"重新分配才会被$(".resultg").text("g = " + g); 识别

问题不奇怪,答案也不奇怪,很简单。这只是全局变量概念

//下面的a,b,c,d,e,f,g是全局变量

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

所以现在a,b,c,d,e,f,g被分配了一个值"out"

因此,如果你试图提醒任何人,它将导致

//您的功能从这里开始

    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);