Function declared in a loop contains unsafe references to variable(s) '$', '$', '$', '$', 'T', '$', 'T', '$', 'T', '$'.
190 if ($('#checkall' + b).is(':checked')) {
191 s.push(T + '.*')
192 } else {
193 $('.checkfield.' + b).each(function (index) {194 const f = 'field' + $(this)[0].id.substr(6)195196 if ($(this).is(':checked')) {197 const box = String($(this)[0].id).split('_')[2]198 const alias = $('#alias' + box).val()199200 if (alias == undefined || alias == '') {201 s.push(T + '.' + $('#' + f).html())202 } else {203 s.push(T + '.' + $('#' + f).html() + ' AS ' + T + '_' + $('#' + f).html())204 }205 }206 })207 }
208 }
209 }
Function declared in a loop contains unsafe references to variable(s) '$', '$'.
809 .attr('data-nu-join', key)
810 .attr('title', jt + ' JOIN ON ' + nuSQL.joins[key].fromfield + ' = ' + nuSQL.joins[key].tofield + ' (Click to Change Join)')
811 .addClass('nuRelationships')
812 .hover(function () {813 $(this).css('border-top-width', 2)814 $(this).css('border-bottom-width', 2)815 }, function () {816 $(this).css('border-top-width', 0)
817 $(this).css('border-bottom-width', 0)
818 })
Function declared in a loop contains unsafe references to variable(s) '$', '$'.
812 .hover(function () {
813 $(this).css('border-top-width', 2)
814 $(this).css('border-bottom-width', 2)
815 }, function () {816 $(this).css('border-top-width', 0)817 $(this).css('border-bottom-width', 0)818 })819
820 var L = $('#' + L.id)
821 const top = parseInt(f.top + f.top - L.top, 10)
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', '$'.
4513 for (var i = 0; i < c.length; i++) {
4514 if (c[i].subform) {
4515 $('#' + c[i].id + 'scrollDiv' + ' > .nuSubform1').remove()
4516 $('#' + c[i].id + 'scrollDiv' + ' > .nuSubform0').each(function () {4517 if ($(this)[0].id != c[i].id + '000nuRECORD') {4518 $(this).remove()4519 }4520 })4521
4522 const k = $('#' + c[i].id + '000nuRECORD').children()
4523
Function declared in a loop contains unsafe references to variable(s) '$', 'i', '$', 'i', 'nuSetSearchColumn'.
4804 .attr('onclick', 'nuClickSearchColumn(event);')
4805 .addClass('nuOptionsItem')
4806 .html(nobr)
4807 .click(function () {4808 const cb = $('#nuSearchList' + i).attr('checked')48094810 $('#' + 'nuSearchList' + i).attr('checked', !cb)48114812 nuSetSearchColumn()4813 })4814
4815 if (i < 9) {
4816 const shortcut_key = document.createElement('div')
Function declared in a loop contains unsafe references to variable(s) 'nuBuildLookup'.
1025 .prop('disabled', false)
1026
1027 if (c === 2) { // -- button
1028 $current.on('click', () => nuBuildLookup(components[c], ''))1029 }
1030 }
1031 })
Function declared in a loop contains unsafe references to variable(s) 'length', 'isEmpty'.
1462
1463 if (arr.length > settings.maxArray) {
1464 for (var i = 0, length = arr.length; i < length; i += settings.maxArray) {
1465 (function (i) {1466 const until = Math.min(i + settings.maxArray, length)1467 table.addRow([1468 i + '..' + (until - 1),1469 util.expander(1470 '[EXPAND]',1471 'Click to show items from this slice',1472 function () {1473 const obj = {}1474 for (let j = i; j < until; j++) {1475 obj[j] = arr[j]1476 }1477 try {1478 const child = prettyPrintThis(obj, {1479 maxDepth: 11480 })1481 child.getElementsByTagName('th')[0].style.display = 'none'1482 this.parentNode.appendChild(child)1483 } catch (e) {1484 this.parentNode.appendChild(1485 util.table(['ERROR OCCURED DURING OBJECT RETRIEVAL'], 'error').addRow([e.message]).node)1486 }1487 })1488 ])1489 isEmpty = false1490 })(i)1491 }
1492 } else {
1493 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 ...