简体   繁体   English

传递给Array.prototype.every的函数中的“可变变量可从闭包访问”

[英]“Mutable variable is accessible from closure” in a function passed to 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

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 . 什么这是做正经历topArray ,并发现,要么阵列,其具有的第一构件innerArray长度为0,否则寻找具有最小长度的一个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? 除了使用for循环而不是every ,还有什么方法可以在没有警告发生的情况下获得我想要的功能?

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. 更新:就其价值而言,警告似乎确实来自我的WebStorm IDE本身,而不是任何分析工具插件。

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. 我很想听听其他答案,但如果没有,我会在大约一周内接受这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM