Skip to content
Permalink
Browse files

Docs: Add modified variable examples for no-loop-func (fixes #9527) (#…

  • Loading branch information...
rachx authored and not-an-aardvark committed Mar 24, 2018
1 parent a9ee9ae commit 692e3830b8e9395529e458447225f26298732d57
Showing with 12 additions and 4 deletions.
  1. +12 −4 docs/rules/no-loop-func.md
@@ -31,6 +31,8 @@ In this case, each function created within the loop returns a different number a

This error is raised to highlight a piece of code that may not work as you expect it to and could also indicate a misunderstanding of how the language works. Your code may run without any problems if you do not fix this error, but in some situations it could behave unexpectedly.

This rule disallows any function within a loop that contains unsafe references (e.g. to modified variables from the outer scope).

Examples of **incorrect** code for this rule:

```js
@@ -52,11 +54,17 @@ do {
} while (i);
let foo = 0;
for (let i=10; i; i--) {
// Bad, function is referencing block scoped variable in the outer scope.
var a = function() { return foo; };
a();
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;
```

Examples of **correct** code for this rule:

0 comments on commit 692e383

Please sign in to comment.
You can’t perform that action at this time.