When is it dangerous to line break after a close parenthesis in Javascript?

后端 未结 4 1818
一生所求
一生所求 2021-01-18 03:37
  • What are the dangers of breaking a line of long code up at a close parenthesis?

  • When could a semicolon be automatically inserted by Javascript (pres

4条回答
  •  清酒与你
    2021-01-18 04:38

    This link should explain it all:

    JavaScript Semicolon Insertion

    The "danger" is with (taken from the above link, emphasis added):

    There are five restricted productions in the grammar, they are the postfix operators ++ and --, continue statements, break statements, return statements, and throw statements.

    function() is not in that "danger" list. However, when writing semi-colon free-code (I'm not sure if this is your aim :-), one should guard against lines starting with characters -- such as ( or [ -- that may start or continue an expression. The following code shows an example of code which is likely wrong:

    x()
    (function (){...})()
    

    As you can see, using ) as a line-breaker may make the expression able to continue on subtly without an explicit semi-colon iff the next line can continue the expression. I write the proceeding as (if the following is indeed the intent):

    x()
    ;(function (){...})()
    

    Personally, I dislike JSLint :-) Happy coding.

提交回复
热议问题