Aditya Sharma created ROL-2157:
----------------------------------
Summary: Variables should be declared explicitly in Custom JS code
Key: ROL-2157
URL: https://issues.apache.org/jira/browse/ROL-2157
Project: Apache Roller
Issue Type: Improvement
Reporter: Aditya Sharma
Assignee: Aditya Sharma
Pattern is identified and reported at sonacloud.io as Blocker
JavaScript variable scope can be particularly difficult to understand and get
right. The situation gets even worse when you consider the _accidental_
creation of global variables, which is what happens when you declare a variable
inside a function or the {{for}} clause of a for-loop without using the
{{let}}, {{const}} or {{var}} keywords.
{{let}} and {{const}} were introduced in ECMAScript 2015, and are now the
preferred keywords for variable declaration.
Noncompliant Code Example
{code:java}
function f(){
i = 1; // Noncompliant; i is global
for (j = 0; j < array.length; j++) { // Noncompliant; j is global now too
// ...
}
}
{code}
Compliant Solution
{code:java}
function f(){
var i = 1;
for (let j = 0; j < array.length; j++) {
// ...
}
}
{code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)