While reading a JavaScript article I came across this term "Shadow Identifier Declaration". Can someone explain what this is? If possible please provide a simple example too.
It's when you declare an identifier in a scope that hides one that exists in the containing scope:
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
// ...
}
There are several ways you might shadow something:
With a variable declaration (var
, let
, const
), as above
With a parameter declaration:
var foo; // The outer one
function example(foo) { // This parameter shadows the outer `foo`
// ...
}
With a function declaration:
var foo; // The outer one
function example() {
function foo() { // This shadows the outer `foo`
}
// ...
}
...and several others. Anything that declares an identifier within a scope that hides (shadows) one in the containing scope, that's a shadowing declaration/definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With