变量范围和变量 [英] Variable Scope and Var

查看:110
本文介绍了变量范围和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一切都从这些简单的代码开始:

  a = 3; 
b = 2;

function line(x){
var a = 5;
var b = 4;

return a * x + b;
}

//返回17
b = line(a) - b;
alert(b);

//返回36
c = line(a)+ b;
alert(c);

两个快讯分别返回17和36。控制按预期工作。直到...



我错误地修改了`
'这样的函数内部:

 函数行(x){
var a = 5;
b = 4;

return a * x + b;
}

突然第13行返回15,第17行返回23

并且情况继续恶化,因为我沿着兔子洞跟随 var
载体,随着我的下降, >

我意识到我可以做一个精神上的注意,总是使用 var

和rest知道我的代码总是按预期工作

但这已经成为一个原则问题,现在我需要

来了解 var 实际上是如何工作的




这里是链接到四个地狱,

由>



Riddle#1
http:/ /jsfiddle.net/js_test/gNEmY/



Riddle#2
http://jsfiddle.net/js_test/FJVYL/



Riddle#3
http://jsfiddle.net/js_test/Vz7Sd/



Riddle#4
http://jsfiddle.net/js_test/RaA5J/



如果任何人都可以给我洞察

到底下发生的事情

可能,每个警报期间会发生什么()呼叫;

我真的很感激。

解决方案

var 创建一个局部变量,范围为出现它的函数。在全局范围中声明的任何变量都是全局对象的属性(在浏览器中, window ),以及在函数中引用的任何变量, code> var 在该函数中指的是周围的范围,很可能是全局范围。



let ,它将在即将到来的ECMAScript版本中出现,并已在某些浏览器中创建块范围的变量。



所以,在你的第一个例子(我假设这是在浏览器中运行),你创建 window.a .b ,它们分别绑定到3和2。函数 line 声明局部变量 a b 。当window.a被传递到行时,参数 x 被绑定到值3(window.a的值)。局部变量 a b 分别为5和4。 这些与window.a和window.b无关。 a * x + b 的计算结果是5 * 3 + 4 ,或19。



代码 b = line(a) - b 通过 .a 到,计算值19,减去 window.b 从它(2),导致17,然后分配给 window.b 。 36的值来自 line(3)(仍然是19)加17(新值 window.b c> > c>

当您将 var code>在函数中,您更改了它,使得 b 不再是局部变量。 在这种情况下,函数中对 b 的所有引用都指向全局值window.b。



在Riddle#2的情况下,在第一次调用window.b等于15后,你有23。当第二次调用line(a)时,window.b设置为4 ,a * x + b计算仍然得到19,然后再次添加window.b(4),得到23。



重要的是要注意, code> var 关键字声明一个变量,这是一个函数作用域,不是块范围,你可能期望从其他C派生语言。例如,在此函数中:

 函数范围(数组){
var a = 7;

for(var b = 0; b var c = array [b];
}

alert(a + b + c);
}

所有变量都具有覆盖整个函数的范围。特别地, c 的范围是 限于 for loop。



但是,未在特定范围中声明的变量不一定引用全局变量。如果它嵌套在另一个函数范围内,它将引用该嵌套的范围。请考虑以下示例:

  var b = 7; 

function outer(){
var b = 42;

function inner(){
return b; //指向外部的b,而不是全局的
}

return inner();
}

alert(outer()); // 42,not 7


It all started with these simple lines of code:

a = 3;
b = 2;

function line(x) {
    var a = 5;
    var b = 4;

    return a*x+b;
}

// returns 17
b = line(a) - b;
alert(b);

// returns 36
c = line(a) + b;
alert(c);

Both alerts return 17 and 36, respectively.
The control works as expected. Until…

I make the mistake of changing
the inside of the function like so:

function line(x) {
    var a = 5; 
        b = 4;

    return a*x+b;
}

Suddenly line 13 returns 15, line 17 returns 23
and the situation continues to deteriorate as I follow var
down the rabbit hole, becoming more enmeshed as I make my descent.

I realize I could make a mental note to always use var
and rest knowing my code will always work as intended
but this has become a matter of principle now and now I need
to understand how var actually works.

Here are links to the four hells,
made by (and possibly for) me:

Riddle #1 http://jsfiddle.net/js_test/gNEmY/

Riddle #2 http://jsfiddle.net/js_test/FJVYL/

Riddle #3 http://jsfiddle.net/js_test/Vz7Sd/

Riddle #4 http://jsfiddle.net/js_test/RaA5J/

If anyone could give me insight
into what's happening under the hood
to wit, what happens during each alert() call;
I would really appreciate it.

解决方案

var creates a local variable, scoped to the function in which it appears. Any variable that is declared in the global scope becomes a property of the global object (in a browser, window), and any variable referenced in a function which is not declared with var in that function refers to the surrounding scope, quite possibly the global scope.

There is also a new keyword, let, which is coming in the upcoming version of ECMAScript and is already in some browsers, which creates a block-scoped variable.

So, in your first example (I am going to assume this is running in a browser), you create window.a and window.b which are bound to 3 and 2, respectively. Function line declares local variables a and b. When window.a is passed to line, the parameter x is bound to the value 3 (the value of window.a). Local variables a and b are 5 and 4, respectively. These have nothing to do with window.a and window.b. The calculation a*x+b is, thus, 5*3+4, or 19.

The code b = line(a) - b passes window.a to line, calculates the value 19, subtracts the current value of window.b from it (2), resulting in 17, which is then assigned to window.b. The value of 36 comes from line(3) (which is still 19) plus 17 (the new value of window.b).

When you removed the var from the assignment to b in function line, you changed it so that b is no longer a local variable. In that case, all references to b in the function refer to the global value window.b.

In the case of your Riddle #2, where you got 23, after the first call window.b is equal to 15. When line(a) is called a second time, window.b is set back to 4, the a*x+b calculation still gets 19, and then window.b (4) is added again, making 23.

It is important to note that the var keyword declares a variable that is function-scoped, not block-scoped as you might expect from other C-derived languages. For example, in this function:

function scope(array) {
    var a = 7;

    for (var b = 0; b < array.length; ++b) {
        var c = array[b];
    }

    alert(a + b + c);
}

All the variables have scope which extends over the entire function. In particular, the scope of c is not limited to the for loop.

A variable that is not declared in a particular scope doesn't necessarily reference a global variable, however. If it is nested inside another function scope, it will refer to that nested scope. Consider the following example:

var b = 7;

function outer() {
    var b = 42;

    function inner() {
        return b; // refers to b in outer, not global
    }

    return inner();
}

alert(outer()); // 42, not 7

这篇关于变量范围和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆