最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Should I be worried about "Missing semicolon" in JSLint? - Stack Overflow

programmeradmin1浏览0评论

I have the following:

$.ajaxSetup({beforeSend:function(a){a.setRequestHeader("Accept","text/javascript")},cache:false});

JSLint is telling me: "Problem at line 1 character 83: Missing semicolon"

Do you agree? I added it, but when I use YUI Compressor it removes it?

Thanks

I have the following:

$.ajaxSetup({beforeSend:function(a){a.setRequestHeader("Accept","text/javascript")},cache:false});

JSLint is telling me: "Problem at line 1 character 83: Missing semicolon"

Do you agree? I added it, but when I use YUI Compressor it removes it?

Thanks

Share Improve this question edited Jun 26, 2015 at 17:21 Sumurai8 20.8k11 gold badges69 silver badges102 bronze badges asked Dec 10, 2010 at 6:22 AnApprenticeAnApprentice 111k202 gold badges637 silver badges1k bronze badges 1
  • I don't use semi-colons, except where required per ASI. However, I also don't write code like that because it's hard to read/parse (for a human) -- with good formatting rules, writing semi-colon free code neither causes additional problems nor confusion. With bad formatting rules, explicit semi-colons are the least of your worries. – user166390 Commented Dec 10, 2010 at 6:52
Add a ment  | 

5 Answers 5

Reset to default 5

JSLint will hurt your feelings.

- Douglas Crockford

And in 90% of the cases it does nothing more, it may plain about the missing semicolon, but the code still works fine, since it's technically not required at that position, so YUI pressor removes it to save the byte.

JSLint tries to detect potential bugs, just as warning signs. As such it should be used on working code, and not pressed code.

The pressor removes it to save bytes being sent (smaller filesize / quicker download).

Optional semicolons can be the cause of problems, e.g.

function a() {

   return // <-- semi colon is inserted here, terminating the line.
     {
         abc: '???'
     }
}

alert(a()); // undefined

I always explicitly add my semi colons, and do BSD KNF style indenting.

Do not worry what the pressor does - if your code runs fine, it is all good.

Update

Pst points out in the ments (cheers) that ASI isn't the problem above - the grammar of return expects the value to be immediately after it. Still, I think ASI could still be a problem there - if each \n gets a ; preceding it automatically.

I whipped up another example of ASI being a PITA.

var a = function(b) {
    
    b.call();  
    
};

(function() {
    c = 'hello',
    a
        
    (function() {
        alert('hello')  
    })
    
})();
    

See it on jsFiddle.

The code looks pretty stupid (why is the a variable floating there)?

A novice coder may not know of var scoping variables, and may build a list of two variables like that. They may then wrap a function in parenthesis (this person is still learning:)).

In the example above, the function will be sent to a() and executed.

A semicolon is not required for correct javascript, but jslint helps you find potential bugs and not necessarily broken javascript. You can safely ignore that error.

Here is required version for JSLint

$.ajaxSetup({beforeSend:function(a){a.setRequestHeader("Accept","text/javascript");},cache:false});

Note: I've added ; to setRequestHeader function call.

发布评论

评论列表(0)

  1. 暂无评论