Mutable variable is accessible from closure

jan

I have the following code:

for (var i = 0; i < data.length; i++) {
        var file = data[i];
        $.getJSON("/types/" + file, function(json) {
            if (json[0] !== undefined) {
                console.log(json[0] + file);
            }
        });
    }

But my editor is saying "Mutable variable is accessible from closure". I've tried to change function(json) { to function(json, file) {, but this don't work because this is a default function of jquery.

I hope you can help my fixing the problem.

Van Coding

For such loops, you need to put the contents in a closure.

for (var i = 0; i < data.length; i++) {
    (function(){
        var file = data[i];
        $.getJSON("/types/" + file, function(json) {
            if (json[0] !== undefined) {
                console.log(json[0] + file);
            }
        });
    })();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Go closure variable scope

来自分类Dev

Firefox in a docker container accessible from selenium in another

来自分类Dev

Can we change the value of a final variable of a mutable class?

来自分类Dev

Why is the value of the instance variable set in a Groovy closure not visible outside of it?

来自分类Dev

Fortran derived types containing derived types to be accessible from C

来自分类Dev

Bash: Update the parent variable from function variable

来自分类Dev

IList <mutable_struct>与mutable_struct []

来自分类Dev

Access class variable from instance

来自分类Dev

Remove a substring from a bash variable

来自分类Dev

Clojure closure efficiency?

来自分类Dev

Scala Closure Free变量

来自分类Dev

Google Closure Editor插件

来自分类Dev

为什么mutable.IndexedSeqLike不扩展mutable.SeqLike?

来自分类Dev

Pass variable from applescript to executable bash file

来自分类Dev

Variable from attribute only displays the first word

来自分类Dev

Reconstruct a categorical variable from dummies in pandas

来自分类Dev

Get data frame from character variable

来自分类Dev

Get/Set Workflow Variable from uiTypeEditor

来自分类Dev

How to execute a GDB command from a string variable?

来自分类Dev

Pulling variable length substring from middle of string

来自分类Dev

Excel: Get max value from variable range

来自分类Dev

Mutable array of immutable length in D

来自分类Dev

F# mutable list is null

来自分类Dev

mutable.Map深层合并

来自分类Dev

Is there a way to test if a closure is also a generator?

来自分类Dev

如何制作Google Closure插件

来自分类Dev

Google Closure库发布管理

来自分类Dev

在Angularjs $ watch中使用Closure

来自分类Dev

Returning a function vs returning a closure

Related 相关文章

热门标签

归档