mirror of
https://github.com/github/codeql.git
synced 2025-12-28 22:56:32 +01:00
41 lines
1.0 KiB
XML
41 lines
1.0 KiB
XML
<!DOCTYPE qhelp PUBLIC
|
|
"-//Semmle//qhelp//EN"
|
|
"qhelp.dtd">
|
|
<qhelp>
|
|
<overview>
|
|
<p>
|
|
JavaScript allows local variables to be used before they are declared. However, this is confusing
|
|
for readers of the code, since it may not be clear whether the use refers to the local variable or
|
|
to a global variable of the same name.
|
|
</p>
|
|
|
|
</overview>
|
|
<recommendation>
|
|
|
|
<p>
|
|
Move local variable declarations to the beginning of the enclosing function, or (for global
|
|
variables) to the beginning of the source file.
|
|
</p>
|
|
|
|
</recommendation>
|
|
<example>
|
|
|
|
<p>
|
|
In the following example, the local variable <code>x</code> declared in function <code>f</code>
|
|
shadows the global variable of the same name. However, the first use of the local variable
|
|
precedes its declaration, so a developer unfamiliar with the code might assume that this use refers
|
|
to the global variable instead of the local variable.
|
|
</p>
|
|
|
|
<sample src="examples/DeclBeforeUse.js" />
|
|
|
|
</example>
|
|
<references>
|
|
|
|
|
|
<li>D. Crockford, <i>JavaScript: The Good Parts</i>, Section 4.9. O'Reilly, 2008.</li>
|
|
|
|
|
|
</references>
|
|
</qhelp>
|