2017-09-23 44 views
0

JSLint不断返回以下错误:预期'{'改为看到'类型',我该如何解决?JSLint - 预期'{'改为看到'类型'

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); 
    } 
} 

回答

2

我想按照规则的JSLint,if应该有括号

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

从JSLint的文档,

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

JSLint的期待与functionifswitchwhilefordotry语句和无处块。

JSLint的期望ifwhiledofor语句将与块{即,具有在大括号}报表进行。

JavaScript允许如果要这样写:

if (condition) 
    statement; 

这形式被称为有助于在项目中的错误,许多程序员都在相同的代码工作。这就是为什么JSLint期望使用一个区块:

if (condition) { 
    statements; 
} 

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

1

试试这个:

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); 
 
    } 
 
}

2

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(); 

虽然有效的问题,线条更加混淆,而且容易出错皮棉,也不会考虑为有效

只是为了让JSLint微笑你可能会重写它到

if (!pfx[p]) {  
type = type.toLowerCase();  
} 
相关问题