javascript - Regarding the issue of semicolon, why is an error reported without a semicolon?
高洛峰
高洛峰 2017-05-16 13:42:00
0
3
498

Output 2

When the semicolon is removed, an error is reported

May I ask what the semicolon does? Why does removing it result in an error?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
我想大声告诉你

It’s not a semicolon problem,
but if there is no blocking of ; sign, the interpreter directly merges the two statements into:

var xx = function(){}[1,2,3].aa(22);

Pay special attention when using statements starting with () and [], because these two operators will be combined with the previous expression first, and the ; sign cannot be omitted

In some cases; sign is not necessary, such as;

var a = 1, b=2
a++
b++

This will not report an error

Since js does not clearly explain such behavior, and the page is easy to be confused, the recommended approach is generally:
Whether it is necessary or not, you'd better add a sentence at the end of the ";" separator, unless you Know exactly what the result is (want).
This is called best practice

洪涛

function(){}[1,2,3] returns undefined

刘奇

The semicolon here should indicate the end of this statement, right?

Array.prototype.aa = function(){console.log(2);};

I think you can tell it when you write it like this.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!