445 const children = record.addedNodes;
446 for(const child of children)
447 {
448 child.querySelectorAll(chatLineParentSelector).forEach(lineParent => {449 chatBody = lineParent.lastChild;450 replaceChatData(chatBody);451 });452 }
453 }
454}
290 */
291 const image = preRenderedIcons.image[icon.nameHash].cloneNode();
292 image.onclick = iconClickHandlerInChat;
293 image.onmouseover = () => {294 tippy(image, {295 hideOnClick: true,296 placement: "top-end",297 theme: "twitch",298 }).show();299 }300 image.onmouseout = () => {
301 image._tippy && image._tippy.destroy();
302 }
67 for(let i=0; i<this.functions.length; i++)
68 {
69 if(this.stop) return;
70 await (async () => {71 if(fail) {72 logger.info(error);73 return;74 };75 await this.functions[i](); 76 })();77 }
78 }
79}
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.
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;
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 ...