javascript - JavaScript 中的 "Shadow Identifier Declaration"是什么?

标签 javascript

阅读时a JavaScript article我遇到了这个术语“影子标识符声明”。有人可以解释这是什么吗?如果可能,请也提供一个简单的示例。

snapshot from the 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
    // ...
}

有几种方法可以遮蔽某些东西:

  1. 有一个变量声明(var, let, const),如上

  2. 带参数声明:

    var foo; // The outer one
    function example(foo) { // This parameter shadows the outer `foo`
        // ...
    }
    
  3. 使用函数声明:

    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/

相关文章:

javascript - MVC 列表转 jquery

javascript - 更改消息框标题

javascript - 如何将数据从Java方法发送到javascript函数?

javascript - 如何在 Vue JS 2 中间接自嵌套组件?

javascript - 使用 Redux/React 移动数组中的项目

javascript - css 和 js 文件的路径

javascript - 如何让一个保管箱显示另一个保管箱中的选项

javascript - 图像结果未显示在页面上

javascript - 警报或 Console.log 无法通过动态加载的 JavaScript 工作

javascript - 尝试在成员加入服务器时添加角色