什么是JavaScript中的"影子标识符声明"?

Aad*_*rma 7 javascript

在阅读JavaScript文章时,我遇到了这个术语"影子标识符声明".有人能解释一下这是什么吗?如果可能的话,请提供一个简单的例子.

文章的快照

T.J*_*der 7

当您在范围中声明一个隐藏包含范围中存在的标识符的标识符时:

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
    // ...
}
Run Code Online (Sandbox Code Playgroud)

有几种方法可能会影响某些东西:

  1. 用变量声明(var,let,const),如上述

  2. 使用参数声明:

    var foo; // The outer one
    function example(foo) { // This parameter shadows the outer `foo`
        // ...
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 带功能声明:

    var foo; // The outer one
    function example() {
        function foo() { // This shadows the outer `foo`
        }
        // ...
    }
    
    Run Code Online (Sandbox Code Playgroud)

......还有其他几个人.在范围内声明标识符的任何东西,它隐藏(阴影)包含范围中的一个,这是一个阴影声明/定义.