Search code examples
javascriptjslint

JSLint -Expected '{' instead saw 'type'


JSLint keeps returning the following error: Expected '{' instead saw 'type', how can I fix it?

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

Solution

  • I think as per JSlint rules, if should have braces

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

    From JSLint docs,

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

    JSLint expects blocks with function, if, switch, while, for, do, and try statements and nowhere else.

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

    JavaScript allows an if to be written like this:

    if (condition)
        statement;
    

    That form is known to contribute to mistakes in projects where many programmers are working on the same code. That is why JSLint expects the use of a block:

    if (condition) {
        statements;
    }
    

    Experience shows that this form is more resilient.