JSlint:意外 'for'

JSlint: unexpected 'for'

我一直在用单选按钮进行测试。在我 运行 通过 JS lint 之前,一切似乎都还不错。我修复了除一个错误之外的所有错误:

意外'for'

for (i = 0; i < radios.length; i += 1) {

这是我的 Javascript:

/*global body,window,document,alert*/
(function () {
"use strict";

var UIlogic;

UIlogic = {
    myLoad: function () {
        var elems, elemText, btn1, winter, summer, fall, spring, header, btns;

        winter = "<div><input type='radio' name='cb' id='cbA' value='0'/><label for='cbA'>Winter</label></div>";
        summer = "<div><input type='radio' name='cb' id='cbB' value='1'/><label for='cbB'>Summer</label></div>";
        fall = "<div><input type='radio' name='cb' id='cbC' value='2'/><label for='cbC'>Fall</label></div>";
        spring = "<div><input type='radio' name='cb' id='cbD' value='3'/><label for='cbD'>Spring</label></div>";

        header = "Header";

        btns = "<br /><button class='btns' id='btn1'>Go!</button>";

        elemText = "Menu/nav";

        elems = "<center><div>" + header + "</div></center>";//title
        elems += "<div>" + elemText + "</div></center>";//menu
        elems += "<div id='container'><br />";//container opens
        elems += "<div id='div1'>" + winter + "</div>";
        elems += "<div id='div2'>" + summer + "</div>";
        elems += "<div id='div2'>" + fall + "</div>";
        elems += "<div id='div2'>" + spring + "</div>";
        elems += "<div id='div3'>" + btns + "</div>";
        elems += "</div>";//container closes
        elems += "<h6><div id='footer'>Ehawk 2015</div></h6>";

        body.innerHTML = elems;
        btn1 = document.getElementById("btn1");
        btn1.addEventListener('click', UIlogic.intoFunction, false);

    },
    intoFunction: function () {
        var radios, found, i = 0;
        radios = document.getElementsByName("cb");
        found = 1;
        for (i = 0; i < radios.length; i += 1) {//issue occurs here
            if (radios[i].checked) {
                alert(radios[i].value);
                found = 0;
                break;
            }
        }
        if (found === 1) {
            alert("Please Select Radio");
        }
    }
};
window.onload = function () {
    UIlogic.myLoad();
};
}());

我 运行 我的循环错了吗?即使代码有效,为什么 JSlint 会在这里看到问题?我真的可以使用一些关于循环的洞察力,因为我对它们有最多的问题。我被告知不要使用它们,但我没有看到 运行 循环检测单选按钮和检查收音机的问题。这是我应该关心的事情吗?

Here's the best explanation I could find for you

Recently I decided I don't need to use for anymore. I'm done with for loops. for loops were invented, they came out of Fortran. They were intended to be a way of working through an array, but in ES5 we added forEach() and map() and all the others, I was like 'Yeah, that's the way to do it'. The for syntax is so weird anyway with the thing with the three statements in it.

Crockford 进一步讨论了在 ES6 中完全使用循环结构,而只使用函数结构。

您可以选择忽略它 - 只需将选项传递给 JSLint 即可容忍 for


但是,假设您决定取消现有的 for 循环。你可以 every()。就像(未测试):

intoFunction: function () {
    var radios, found; 
    radios = document.getElementsByName("cb");
    found = Array.prototype.slice.call(radios).every(function(radio) {
        if (radio.checked) { 
            alert(radio.value); 
            return false; 
        }
        return true; 
    }); 

    if (found) {
        alert("Please Select Radio");
    }
}

老实说,我认为 for 循环是否更容易理解。老实说,这取决于您自己的个人/项目的编码标准。


更新了一个演示 every() 以实现此目的的工作片段。

function into() {
  var radios, found;
  radios = document.getElementsByName("cb");
  found = Array.prototype.slice.call(radios).every(function(radio) {
    if (radio.checked) {
      alert(radio.value);
      return false;
    }
    return true;
  });

  if (found) {
    alert("Please Select Radio");
  }
}

jQuery("[name='cb']").on("click", into);
jQuery("button").on("click", function() {
  jQuery("[name='cb']").prop("checked", false);
  into();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
  <label>
    <input type="radio" name="cb" value="M">Male</label>
</p>
<p>
  <label>
    <input type="radio" name="cb" value="F">Female</label>
</p>
<p>
  <label>
    <input type="radio" name="cb" value="I">I don't know</label>
</p>
<p>
  <label>
    <input type="radio" name="cb" value="B">Both</label>
</p>
<p>
  <button>Clear Radios</button>

JSLint 建议您使用其他循环,例如 forEach。 http://www.jslint.com/help.html#for

如果这让您感到困扰,您可以 select 容忍底部的 "for statement" 选项,但代码看起来不错。