
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
59 lines
1.2 KiB
Plaintext
59 lines
1.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Variables declared with ``++var++`` have the special property that regardless of where they're declared in a function they "float" to the top of the function and are available for use even before they're declared. That makes scoping confusing, especially for new coders.
|
|
|
|
To keep confusion to a minimum, ``++var++`` declarations should happen before they are used for the first time.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,javascript]
|
|
----
|
|
var x = 1;
|
|
|
|
function fun(){
|
|
alert(x); // Noncompliant as x is declared later in the same scope
|
|
if(something) {
|
|
var x = 42; // Declaration in function scope (not block scope!) shadows global variable
|
|
}
|
|
}
|
|
|
|
fun(); // Unexpectedly alerts "undefined" instead of "1"
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,javascript]
|
|
----
|
|
var x = 1;
|
|
|
|
function fun() {
|
|
print(x);
|
|
if (something) {
|
|
x = 42;
|
|
}
|
|
}
|
|
|
|
fun(); // Print "1"
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Move the declaration of "xxx" before this usage.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
* Primary: usage identifier
|
|
* Additional: declaration identifier
|
|
** Message: Declaration
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|