在阅读JavaScript文章时,我遇到了这个术语"影子标识符声明".有人能解释一下这是什么吗?如果可能的话,请提供一个简单的例子.
当您在范围中声明一个隐藏包含范围中存在的标识符的标识符时:
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)
有几种方法可能会影响某些东西:
用变量声明(var,let,const),如上述
使用参数声明:
var foo; // The outer one
function example(foo) { // This parameter shadows the outer `foo`
// ...
}
Run Code Online (Sandbox Code Playgroud)带功能声明:
var foo; // The outer one
function example() {
function foo() { // This shadows the outer `foo`
}
// ...
}
Run Code Online (Sandbox Code Playgroud)......还有其他几个人.在范围内声明标识符的任何东西,它隐藏(阴影)包含范围中的一个,这是一个阴影声明/定义.
| 归档时间: |
|
| 查看次数: |
817 次 |
| 最近记录: |