What sort of 'strange things' can happen w

2019-02-19 09:55发布

问题:

This question already has an answer here:

  • What are the rules for JavaScript's automatic semicolon insertion (ASI)? 4 answers

In Eloquent Javascript, Marijn Haverbeke writes:

In some cases, JavaScript allows you to omit the semicolon at the end of a statement. In other cases, it has to be there or strange things will happen. The rules for when it can be safely omitted are complex and weird. In this book, I won't leave out any semicolons, and I strongly urge you to do the same in your own programs.

What are some of the strange things that may happen when omitting the semicolon from the end of a statement?

回答1:

/* this */
return
a + b

    /* becomes this: */
    return;
    a + b;

/* this */
a = b
++c

    /* becomes this */
    a = b;
    ++c;

/* this */
a = b + c
(d + e).print()

    /* becomes this */
    a = b + c(d + e).print();

http://lucumr.pocoo.org/2011/2/6/automatic-semicolon-insertion/