JSLint - 预期 '{' 而不是看到 'type'

JSLint -Expected '{' instead saw 'type'

JSLint 不断返回以下错误:Expected '{' instead saw 'type',我该如何解决?

var pfx = ['webkit', 'moz', 'MS', 'o', '']; 
function prefixedEventListener(element, type, callback) {
    for (var p = 0; p < pfx.length; p++) {
      if (!pfx[p]) type = type.toLowerCase();
      element.addEventListener(pfx[p]+type, callback, false);
    }
} 

我认为按照 JSlint 规则,if 应该有大括号

if (!pfx[p]) {
  type = type.toLowerCase();
}

来自 JSLint 文档,

(http://www.jslint.com/help.html)

JSLint 期望包含 functionifswitchwhilefordo 和 [=20= 的块] 声明,别无他处。

JSLint 期望 ifwhiledofor 语句将使用块 { 进行,即用大括号括起来的语句}.

JavaScript 允许 if 写成这样:

if (condition)
    statement;

众所周知,在许多程序员使用相同代码的项目中,这种形式会导致错误。这就是 JSLint 期望使用块的原因:

if (condition) {
    statements;
}

经验表明这种形式更有弹性。

来自Lint docs

JSLint expects that if, while, do and for statements will be made with blocks {that is, with statements enclosed in braces}.

所以线路有问题

if (!pfx[p]) type = type.toLowerCase();

虽然有效,但对于 lint 来说,该行更容易混淆和出错,它不会将其视为有效的

为了让 JSLint 微笑,您可以将其重写为

if (!pfx[p]) {    
type = type.toLowerCase();    
}