简体   繁体   English

这是 JavaScript 中变量阴影的一个例子吗?

[英]Is this an example of variable shadowing in JavaScript?

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 (Chapter 3)中了解了术语变量阴影,但我试图理解这个概念的一个精确的基本示例。

Is this an example of shadowing?这是阴影的例子吗?

 var currencySymbol = "$"; function showMoney(amount) { var currencySymbol = "€"; console.log(currencySymbol + amount); } showMoney("100");

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.至于您的具体问题:是的,这是可变阴影的一个很好的例子。

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.没有该变量声明的其他函数将使用全局函数。

Yes, your example is an example of shadowing.是的,您的示例是阴影的示例。

The shadowing will persist in other scenarios too due to how closures work in JavaScript.由于闭包在 JavaScript 中的工作方式,阴影也会在其他场景中持续存在。 Here's an example:这是一个例子:

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的引用,并且该内部函数的调用对全局没有影响,即使原始函数早已超出范围。

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) 将检查本地范围,如果它找到它使用它的名称变量,如果没有,它会在找到它后检查父范围,它使用那个,所以它不会进入根。

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.请注意最后一行代码(console.log)如何打印developer但它不在函数范围内。 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.这是一个很好的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 关键字声明,以便它只能被函数访问。

Yes, this is a good example of shadowing.是的,这是阴影的一个很好的例子。 A global scope variable is said to be shadowed by a block scope variable when both have the same name.当一个全局范围变量具有相同的名称时,就被称为被块范围变量所遮蔽。 This is happening in your code and the block scope is shadowing the global scope.这发生在您的代码中,并且块范围正在遮蔽全局范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM