Javascript javascript中变量阴影的一个例子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11901427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An example of variable shadowing in javascript
提问by fakeguybrushthreepwood
I learnt about the term variable shadowing in Eloquent Javascript (Chapter 3), but I am trying to understand a precise, basic example of the concept.
我在Eloquent Javascript(第 3 章)中了解了术语变量阴影,但我试图理解该概念的一个精确的基本示例。
Is this an example of shadowing?
这是阴影的一个例子吗?
var currencySymbol = "$";
function showMoney(amount) {
var currencySymbol = "";
document.write(currencySymbol + amount);
}
showMoney("100");?
回答by Madara's Ghost
That is also what is known as variable scope.
这也就是所谓的变量范围。
A variable only exists within its containing function/method/class, and those will override any variables which belong to a wider scope.
一个变量只存在于其包含的函数/方法/类中,这些将覆盖属于更广泛范围的任何变量。
That's why in your example, a euro sign will be shown, and not a dollar. (Because the currencySymbol
containing the dollar is at a wider (global) scope than the currencySymbol
containing the euro sign).
这就是为什么在您的示例中,将显示欧元符号,而不是美元。(因为currencySymbol
包含美元的范围比currencySymbol
包含欧元符号的范围更广)。
As for your specific question: Yes, that is a good example of variable shadowing.
至于你的具体问题:是的,这是变量阴影的一个很好的例子。
回答by Eric Robinson
In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. This outer variable is said to be shadowed...
在计算机编程中,当在特定范围(决策块、方法或内部类)内声明的变量与在外部范围内声明的变量具有相同名称时,就会发生变量影子。据说这个外部变量是隐藏的......
so I believe your example is good.
所以我相信你的例子很好。
you have a globally named variable that shares the same name as inner method. the inner variable will be used only in that function. Other functions without that variable declaration will use the global one.
您有一个与内部方法同名的全局命名变量。内部变量将仅在该函数中使用。没有该变量声明的其他函数将使用全局变量。
回答by Platinum Azure
Yes, your example is an example of shadowing.
是的,您的示例是阴影的示例。
The shadowing will persist in other scenarios too due to how closures work in JavaScript. Here's an example:
由于闭包在 JavaScript 中的工作方式,阴影也会在其他场景中持续存在。下面是一个例子:
var x = -1;
function xCounter() {
var x = 0;
return function() {
++x;
return x;
};
}
console.log(x); // -1
counter = xCounter();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(x); // still -1, global was never touched
Note that in this case, even when xCounter returns, the function it returns still has a reference to its own x
and invocations of that inner function have no effect on the global, even though the original has long since gone out of scope.
请注意,在这种情况下,即使 xCounter 返回,它返回的函数仍然具有对其自身的引用,x
并且对该内部函数的调用对全局没有影响,即使原始函数早已超出范围。
回答by Yilmaz
We cannot define a variable more than once. But we can define in different scopes.
我们不能多次定义一个变量。但是我们可以在不同的范围内定义。
let name="tara"
if(true){
let name="ali"
if(true){
console.log(name)
}
}
variable shadowing is when a variable in a local scope uses its value instead of a variable in a parent scope.So the local variables value is shadowing over the parents.
变量遮蔽是当局部作用域中的变量使用其值而不是父作用域中的变量时。因此局部变量值遮蔽了父作用域。
in the above code there are two name variables defined but they are not defined in the same scope. so console.log(name) will check the local scope if it finds the name variable it uses it, if not it checks parent scope once finds it, it uses that one so it does not go the root.
在上面的代码中定义了两个名称变量,但它们没有定义在同一个范围内。所以 console.log(name) 将检查本地范围,如果它找到它使用的名称变量,如果没有,它会在找到它后检查父范围,它使用那个,所以它不会进入根。
回答by Musty
var role = "Engineer";
console.log(role);
function displayRole(){
role = "developer";
console.log(role);
}
displayRole();
console.log(role);
Notice how the last line of code (console.log) prints developer
yet it's not inside the function scope. This is a good example of shadowing
where by the role variable in the global scope has been overwritten by the role in the function scope.
注意最后一行代码 (console.log) 如何打印developer
但它不在函数范围内。这是shadowing
全局作用域中的角色变量被函数作用域中的角色覆盖的一个很好的例子。
To avoid shadowing, the variable in the function scope should be declared using the var keyword so that it becomes accessible to the function only.
为了避免阴影,函数作用域中的变量应该使用 var 关键字声明,以便它只能被函数访问。