为什么两个函数调用的括号之间的换行符不被视为 js 中的两个语句? [英] why a newline between parentheses of two function calls not treat as two statements in js?

查看:20
本文介绍了为什么两个函数调用的括号之间的换行符不被视为 js 中的两个语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在js上做出这种糟糕的设计?像这样设计自动分号插入有什么特殊原因吗?

这是我的代码,它在 chrome 中的 js 中不起作用:

Here is my code, It is not work in js in chrome:

(function(){console.log("abc");})()

(function(){console.log("123");})();

这里是错误:

Uncaught TypeError: (intermediate value)(...) is not a function

我知道这段代码的正确版本是:

I know the right version of this code is :

(function(){console.log("abc");})();

(function(){console.log("123");})();

我只想知道为什么js语法设计得这么蠢.历史原因?

I just want to know why js syntax is designed so stupid. History reason?

我也添加这个问题作为警告大家尝试使用javascript的自动分号插入,请在需要的地方添加;,javascript的自动分号插入是垃圾.它不像您期望的那样工作.

I also add this question as a warning to everybody try to use the automatic semicolon insertion of javascript, please just add ; everywhere it needs, the automatic semicolon insertion of javascript is rubbish. It does not work as your expect.

存在的答案对我来说太复杂了,所以我问了一个新的:

The exist answer is too complex to my case, so I ask a new one:

https://stackoverflow.com/a/2846298/1586797

另一个看起来不错但不起作用的案例 2:

x=1

(function(){console.log("123");})()

推荐答案

链接问题的答案解释了 ASI 规范的三个规则,例如 这个.tl;博士:

The linked question's answers explain the spec's three rules for ASI, for example this one. tl;dr:

  • 如果它不起作用,请尝试使用分号.

  • If it doesn't work, try with semicolon.

程序应该以分号结束.

如果一个语句说不能把换行符放在这里",用分号惩罚它.

If a statement says "can't put newline here", punish it with semicolon.

您的代码不满足任何条件.

Your code does not satisfy any of the criteria.

  • 第一行可以返回一个函数,如果是这样,应该允许调用该函数;所以 ( 第二行开始不是非法的

  • The first line could return a function, and if so that function should be allowed to be invoked; so ( that the second line begins with is not illegal

第一行不是程序的最后一行

The first line is not the last line of the program

这里没有限制语法.

因此,您没有自动分号.

Therefore, no automatic semicolon for you.

因此有些人声称虽然 (f(){})() 语法是良好的 IIFE 语法,但使用 !f(){}() 代替:

Some people have thus claimed that while (f(){})() syntax is good IIFE syntax, it might be good to do !f(){}() instead:

!function(){console.log("abc");}()

!function(){console.log("123");}();

这按预期工作是因为 ! 只是否定了函数应用程序的(丢弃的)结果,还因为 ! 作为纯粹的一元运算符是继续执行的非法字符第一行(即 f(){}()! 不是一回事).这会触发规则 1,并且 ASI 可以发生.

This works as intended because ! just negates the (discarded) result of the function application, and also because ! as purely unary operator is an illegal character to continue the first line (i.e. f(){}()! is not a thing). This triggers rule 1, and ASI can take place.

反驳说它很丑(即对于不熟悉这种做法的人来说,他们需要一段时间才能理解这个习语中 ! 的目的).

The counterargument is that it is butt-ugly (i.e. for anyone not already familiar with the practice, it takes a while for them to understand the purpose of ! in this idiom).

你的第二个例子本质上是相似的:就 JS 解析器而言,1 是一个值(它是一个整数并且不可能是一个函数的事实有点丢失到它).看看这个在语法上完全等同于你的例子:

Your second example is similar in nature: as far as the JS parser is concerned, 1 is a value (the fact that it is an integer and could not possibly be a function is a bit lost to it). Look at this example that syntactically is completely equivalent to yours:

a=function(f) { console.log("A CALLED!"); return f; }
x=a
(function(){console.log("123");})()
# => A CALLED!
     123

这里,a 是一个函数,所以它可以用 function(){console.log("123");} 作为参数调用;它在打印到控制台后返回 function(){console.log("123");} 不变;然后 () 调用返回值,并打印 123 .一切正常.因此,不会触发规则 #1,也没有分号.

Here, a is a function, so it can be invoked with function(){console.log("123");} as an argument; it returns function(){console.log("123");} unchanged after printing to the console; then () invokes that return value, and 123 is printed as well. Everything works. Thus, Rule #1 is not triggered, no semicolon for you.

这篇关于为什么两个函数调用的括号之间的换行符不被视为 js 中的两个语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆