“Mutable variable is accessible from closure”在传递给Array.prototype.every的函数中 [英] "Mutable variable is accessible from closure" in a function passed to Array.prototype.every

查看:1567
本文介绍了“Mutable variable is accessible from closure”在传递给Array.prototype.every的函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码会比我更清楚地说:

Code will speak more plainly than I will:

var candidateIndex = 0;
var minValue = Number.MAX_VALUE;
topArray.every(function(element, index) {
    if (element.innerArray && element.innerArray.length < minValue) {
        minValue = element.innerArray.length;
        candidateIndex = index;
        if (minValue == 0) {
            return false;
        }
    }
    return true;
});

// ... use minValue and candidateIndex

正在通过 topArray ,并找到该数组的第一个成员,该成员具有长度为0的 innerArray ,否则找到具有最小长度 innerArray 的那个。它的工作正常,但检查器,我已准确地报告可变变量是可访问从闭合。

What this is doing is going through the topArray, and finding either the first member of that array that has an innerArray of length 0, otherwise finding the one that has the smallest length innerArray. It's working fine, but the checker that I have accurately reports "Mutable variable is accessible from closure."

我看到这通常是一件坏事,特别是对于异步代码。我浏览了如何避免访问可变变量闭包在事件闭包中访问可变变量,并且理解在这些情况下,匿名函数是异步的,并且期望在该时间存储可变变量的状态,但在我的情况下,我希望我调用的同步匿名函数改变变量。

I see that that's usually a bad thing, particularly with asynchronous code. I've looked through How to avoid access mutable variable from closure and accessing mutable variable in an event closure, and understand that in those cases, the anonymous function is asynchronous, and it's desirable to store the state of the mutable variable at the time, but in my case, I want the synchronous anonymous function I invoke to change the variable.

在这种情况下,我收到的警告是错误的,我应该忽略它,对吧?除了使用代替循环代替每个,有没有办法获得我想要的功能, ?

In this case, the warning that I'm getting is wrong, and I should just ignore it, right? Outside of using a for loop instead of every, is there any way to get the functionality I want without the warning occuring?

更新:值得,警告似乎来自我的WebStorm IDE本身,而不是任何分析工具插件。

Update: for what it's worth, the warning does seem to be coming from my WebStorm IDE itself, instead of any of the analysis tool plugins.

推荐答案

从上面的评论得到这个警告基本上是假阳性的确认,我修改代码忽略警告讯息:

Having gotten confirmation from the comments above that this warning is basically a false positive, I modified the code to ignore the warning message:

topArray.every(function(element, index) {
    //noinspection JSReferencingMutableVariableFromClosure
    if (element.innerArray && element.innerArray.length < minValue) {
        minValue = element.innerArray.length;
        candidateIndex = index;
        //noinspection JSReferencingMutableVariableFromClosure
        if (minValue == 0) {
            return false;
        }
    }
    return true;
});

(比较值时触发,而不是设置时触发。)

(The warning only triggers when comparing the value, as opposed to when setting it.)

我渴望听到任何其他答案,但如果我没有,我会在大约一个星期内接受这个答案。

I'm eager to hear any other answers, but if I don't, I'll accept this answer in about a week.

这篇关于“Mutable variable is accessible from closure”在传递给Array.prototype.every的函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆