Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript var a=b=2和var a=2之间的区别是什么;var b=2;_Javascript_Variables_Global Variables - Fatal编程技术网

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

Javascript var a=b=2和var a=2之间的区别是什么;var b=2;,javascript,variables,global-variables,Javascript,Variables,Global Variables,当我在函数中声明变量时,我面临一个问题 var b = 44; function test(){ var a = b = 2; } 但这很好: var b = 44; function test(){ var a; var b = 2; } 变量b凌驾于全局b变量之上 我找不到有关此行为的任何文档 有关于它的文件吗 演示:我认为这是因为您在函数中声明了$(“.resultg”).text(“g=“+g”)无法识别的变量 但是在vard=e=f=“in”您正在声明

当我在函数中声明变量时,我面临一个问题

var b = 44;
function test(){
    var a = b = 2;
}
但这很好:

 var b = 44;
 function test(){
    var a;
    var b = 2;
 }
变量b凌驾于全局b变量之上

我找不到有关此行为的任何文档

有关于它的文件吗


演示:

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


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

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


但是在
vard=e=f=“in”
您正在声明一个新的局部变量“d”,它等于全局变量
e
f
,然后将“in”分配给所有这些变量。由于
e
f
是全局变量,只有在这种情况下,
$(“.resultg”).text(“g=“+g”)才能识别“in”重新分配
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
不会覆盖外部作用域中声明的变量。让我解释一下
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 a = b = [1,2,3,4];
相当于:

var b = 2;
var a = b;
var b = [1,2,3,4];
var a = b;
如您所知,
a
b
都被分配了相同的值

但是,当指定对象而不是基本体值时:

var a = b = 2;
var a = b = [1,2,3,4];
这也相当于:

var b = 2;
var a = b;
var b = [1,2,3,4];
var a = b;
这意味着
a
b
共享相同的引用。 因此,您对
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
产生的效果与

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

请注意

使用基元值声明时:

var a = b = 2;
var a = b = [1,2,3,4];
相当于:

var b = 2;
var a = b;
var b = [1,2,3,4];
var a = b;
如您所知,
a
b
都被分配了相同的值

但是,当指定对象而不是基本体值时:

var a = b = 2;
var a = b = [1,2,3,4];
这也相当于:

var b = 2;
var a = b;
var b = [1,2,3,4];
var a = b;
这意味着
a
b
共享相同的引用。 因此,您对
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
产生的效果与

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

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

本地>全球

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

var a = b = 2;
您正在创建一个新变量a,该变量接受全局变量b的值,同时将b的值更改为2->您正在覆盖其值

当您写入(内部测试())时:

您正在声明另外两个变量,它们仅在函数内部已知,并且,如果您写入b=2,则可能会面临两种情况:

  • 如果您在test()内使用console.log(b),您将获得2
  • 如果您在test()之外使用console.log(b),您将获得44
  • 声明!=作业

    非常非常重要->

    • 变量a,b是一种声明

    • a=b=25是一项作业(我认为是双重作业)

    • var a=b=25同时是一个声明和一个赋值

    我希望有帮助!!!:)如果有什么不清楚或者你需要任何其他解释,请告诉我

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

    本地>全球

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

    var a = b = 2;
    
    您正在创建一个新变量a,该变量接受全局变量b的值,同时将b的值更改为2->您正在覆盖其值

    当您写入(内部测试())时:

    您正在声明另外两个变量,它们仅在函数内部已知,并且,如果您写入b=2,则可能会面临两种情况:

  • 如果您在test()内使用console.log(b),您将获得2
  • 如果您在test()之外使用console.log(b),您将获得44
  • 声明!=作业

    非常非常重要->

    • 变量a,b是一种声明

    • a=b=25是一项作业(我认为是双重作业)

    • var a=b=25同时是一个声明和一个赋值

    我希望有帮助!!!:)如果有什么不清楚或者你需要任何其他解释,请告诉我

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

    //下面的
    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);
    

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

    //下面的
    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);
    

    你的函数没有返回任何东西,所以