Function declared in a loop contains unsafe references to variable(s) 'i', 'i', 'nuSetSearchColumn'.
4621 .attr('onclick', 'nuClickSearchColumn(event);')
4622 .addClass('nuOptionsItem')
4623 .html(nobr)
4624 .click(function () {4625 const cb = $('#nuSearchList' + i).attr('checked')46264627 $('#' + 'nuSearchList' + i).attr('checked', !cb)46284629 nuSetSearchColumn()4630 })4631
4632 if (i < 9) {
4633 const shortcut_key = document.createElement('div')
Function declared in a loop contains unsafe references to variable(s) 'i'.
4339 for (var i = 0; i < c.length; i++) {
4340 if (c[i].subform) {
4341 $('#' + c[i].id + 'scrollDiv' + ' > .nuSubform1').remove()
4342 $('#' + c[i].id + 'scrollDiv' + ' > .nuSubform0').each(function () {4343 if ($(this)[0].id != c[i].id + '000nuRECORD') {4344 $(this).remove()4345 }4346 })4347
4348 const k = $('#' + c[i].id + '000nuRECORD').children()
4349
Function declared in a loop contains unsafe references to variable(s) 'window', 'window'.
3729 const dataRow = $(this).attr('data-nu-row')
3730 $("[data-nu-row='" + dataRow + "']").not('.nuCellColored').addClass('nuSelectBrowse').removeClass('nuBrowseTable')
3731 }
3732 }, function () {3733 if (window.nuBROWSERESIZE.moving_element == '') {3734 $('[data-nu-row]').addClass('nuBrowseTable').removeClass('nuSelectBrowse')37353736 window.nuBROWSEROW = -137373738 const dataRow = $(this).attr('data-nu-row')3739 $("[data-nu-row='" + dataRow + "']").addClass('nuBrowseTable').removeClass('nuSelectBrowse')3740 }3741 })3742 }
3743
3744 if (r == 0 && c == 0) {
Function declared in a loop contains unsafe references to variable(s) 'window', 'window'.
3716 .attr('onclick', 'nuSelectBrowse(event, this)')
3717 .hover(
3718
3719 function () {3720 if (window.nuBROWSERESIZE.moving_element == '') {3721 if (this.offsetWidth < this.scrollWidth && !$(this).is('[title]')) {3722 $(this).attr('title', $(this).html().replace(/(<([^>]+)>)/ig, '')) // Remove HTML tags3723 }37243725 $('[data-nu-row]').addClass('nuBrowseTable').removeClass('nuSelectBrowse')37263727 window.nuBROWSEROW = -137283729 const dataRow = $(this).attr('data-nu-row')3730 $("[data-nu-row='" + dataRow + "']").not('.nuCellColored').addClass('nuSelectBrowse').removeClass('nuBrowseTable')3731 }3732 }, function () {3733 if (window.nuBROWSERESIZE.moving_element == '') {
3734 $('[data-nu-row]').addClass('nuBrowseTable').removeClass('nuSelectBrowse')
3735
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 ...