Function declared in a loop contains unsafe references to variable(s) 'nuFORM', 'event'.
64 for (let i = 0; i < flds.length; i++) {
65 $(flds[i])
66 .addClass('nuScroll')
67 .keydown(function () { 68 nuFORM.scrollList(event, l) 69 }) 70 }
71}
72
Function declared in a loop contains unsafe references to variable(s) '$', 'i', '$'.
4546 for (var i = 0; i < c.length; i++) {
4547 if (c[i].subform) {
4548 $('#' + c[i].id + 'scrollDiv' + ' > .nuSubform1').remove()
4549 $('#' + c[i].id + 'scrollDiv' + ' > .nuSubform0').each(function () {4550 if ($(this)[0].id != c[i].id + '000nuRECORD') {4551 $(this).remove()4552 }4553 })4554
4555 const k = $('#' + c[i].id + '000nuRECORD').children()
4556
Function declared in a loop contains unsafe references to variable(s) '$', 'i', '$', 'i', 'nuSetSearchColumn'.
4852 .attr('onclick', 'nuClickSearchColumn(event);')
4853 .addClass('nuOptionsItem')
4854 .html(nobr)
4855 .click(function () {4856 const cb = $('#nuSearchList' + i).attr('checked')48574858 $('#' + 'nuSearchList' + i).attr('checked', !cb)48594860 nuSetSearchColumn()4861 })4862
4863 if (i < 9) {
4864 const shortcut_key = document.createElement('div')
Function declared in a loop contains unsafe references to variable(s) 'nuBuildLookup'.
1028 .prop('disabled', false)
1029
1030 if (c === 2) { // -- button
1031 $current.on('click', () => nuBuildLookup(components[c], ''))1032 }
1033 }
1034 })
Function declared in a loop contains unsafe references to variable(s) 'length', 'isEmpty'.
1466
1467 if (arr.length > settings.maxArray) {
1468 for (var i = 0, length = arr.length; i < length; i += settings.maxArray) {
1469 (function (i) {1470 const until = Math.min(i + settings.maxArray, length)1471 table.addRow([1472 i + '..' + (until - 1),1473 util.expander(1474 '[EXPAND]',1475 'Click to show items from this slice',1476 function () {1477 const obj = {}1478 for (let j = i; j < until; j++) {1479 obj[j] = arr[j]1480 }1481 try {1482 const child = prettyPrintThis(obj, {1483 maxDepth: 11484 })1485 child.getElementsByTagName('th')[0].style.display = 'none'1486 this.parentNode.appendChild(child)1487 } catch (e) {1488 this.parentNode.appendChild(1489 util.table(['ERROR OCCURED DURING OBJECT RETRIEVAL'], 'error').addRow([e.message]).node)1490 }1491 })1492 ])1493 isEmpty = false1494 })(i)1495 }
1496 } else {
1497 util.forEach(arr, function (item, index) {
Description
Writing functions within loops tends to result in errors due to the way the function creates a closure around the loop. For example:
for (var i = 0; i < 10; i++)
funcs[i] = function() {
return i;
};
}
In this case, you would expect each function created within the loop to return a different number. In reality, each function returns 10, because that was the last value of i in the scope.
let
or const
mitigate this problem.
for (let i = 0; i < 10; i++) {
funcs[i] = function() {
return i;
};
}
In this case, each function created within the loop returns a different number as expected.
Bad Practice
for (let i = 10; i; i--) {
(function() { return i; })();
}
while(i) {
const a = function() { return i; };
a();
}
do {
function a() { return i; };
a();
} while (i);
let foo = 0;
for (let i = 0; i < 10; ++i) {
//Bad, `foo` is not in the loop-block's scope and `foo` is modified in/after the loop
setTimeout(() => console.log(foo));
foo += 1;
}
for (let i = 0; i < 10; ++i) {
//Bad, `foo` is not in the loop-block's scope and `foo` is modified in/after the loop
setTimeout(() => console.log(foo));
}
foo = 100;
Recommended
let a = function() {};
for (let i = 10; i; i--) {
a();
}
for (let i = 10; i; i--) {
const a = function() {}; // OK, no references to variables in the outer scopes.
a();
}
for (let i = 10; i; i--) {
const a = function() { return i; }; // OK, all references are referring to block scoped variables in the loop.
a();
}
const foo = 100;
for (let i = 10; i; i--) {
const a = function() { return foo; }; // OK, all references are referring to never modified variables.
a();
}
//... no modifications of foo after this loop ...