阅读时a JavaScript article我遇到了这个术语“影子标识符声明”。有人可以解释这是什么吗?如果可能,请也提供一个简单的示例。
最佳答案
当你在一个范围内声明一个标识符时,它隐藏了包含范围中存在的标识符:
var foo; // The outer one
function example() {
var foo; // The inner one -- this "shadows" the outer one, making the
// outer one inaccessible within this function
// ...
}
有几种方法可以遮蔽某些东西:
有一个变量声明(
var
,let
,const
),如上带参数声明:
var foo; // The outer one function example(foo) { // This parameter shadows the outer `foo` // ... }
使用函数声明:
var foo; // The outer one function example() { function foo() { // This shadows the outer `foo` } // ... }
...和其他几个。任何在包含范围内隐藏(遮蔽)标识符的范围内声明标识符的内容,都是遮蔽声明/定义。
关于javascript - JavaScript 中的 "Shadow Identifier Declaration"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42174100/