
The languages for this rule fall into two categories: * CFamily, JS, and PLSQL: "Variables should not be shadowed" (general case of shadowing) * C#, Flex, Java, PHP, Swift: "Local variables should not shadow field/property/enum case/..." (narrow case of shadowing) For CFamily, these tickets are also handled: CPP-2785 CPP-3589
39 lines
907 B
Plaintext
39 lines
907 B
Plaintext
include::../why-general.adoc[]
|
|
|
|
Note that functions in JavaScript are first-class citizens. This means that they possess the same attributes as variables, including the ability to shadow other variables and, conversely, be shadowed by them.
|
|
|
|
=== Noncompliant code example
|
|
|
|
The example below shows the typical situations in which shadowing can occur.
|
|
|
|
[source,javascript]
|
|
----
|
|
function outer(items) {
|
|
var counter = 0;
|
|
|
|
function inner(items) { // Noncompliant: the parameter "items" is shadowed.
|
|
var counter = counter + 1; // Noncompliant: the outer "counter" is shadowed.
|
|
}
|
|
|
|
inner(items);
|
|
|
|
return counter; // returns 0
|
|
}
|
|
|
|
function search(items, match) { // Noncompliant: the function "match" (below) is shadowed.
|
|
// ...
|
|
}
|
|
|
|
function match(value, key) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Related rules
|
|
|
|
* S2814 - Variables and functions should not be redeclared
|
|
|
|
include::../rspecator.adoc[]
|