What sort of 'strange things' can happen w

2019-02-19 09:30发布

This question already has an answer here:

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条回答
家丑人穷心不美
2楼-- · 2019-02-19 10:01
/* 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/

查看更多
登录 后发表回答