74 lines
1.0 KiB
Plaintext
74 lines
1.0 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Variables that are declared inside a block but used outside of it (which is possible with a ``++var++``-style declaration) should be declared outside the block.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,javascript]
|
|
----
|
|
function doSomething(a, b) {
|
|
if (a > b) {
|
|
var x = a - b; // Noncompliant
|
|
}
|
|
|
|
if (a > 4) {
|
|
console.log(x);
|
|
}
|
|
|
|
for (var i = 0; i < m; i++) { // Noncompliant, both loops use same variable
|
|
}
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
}
|
|
|
|
return a + b;
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,javascript]
|
|
----
|
|
function doSomething(a, b) {
|
|
var x;
|
|
|
|
if (a > b) {
|
|
x = a - b;
|
|
}
|
|
|
|
if (a > 4) {
|
|
console.log(x);
|
|
}
|
|
|
|
for (let i = 0; i < m; i++) {
|
|
}
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
}
|
|
|
|
|
|
return a + b;
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|