JavaScript 函数后的空格
我知道空格在 JavaScript 中是无关紧要的,但我对样式很好奇。
定义如下函数时:
function Foo(a, b, c) {}
我不会在函数名称后面添加空格。但是,如果我创建一个函数作为表达式:
Bar(function (a, b, c) {
// do something
})
或者
{
Foo: function (a, b, c) {
// do something
}
}
我发现自己自然地键入了一个空格。我认为这是因为我已经训练自己在函数关键字(或一般关键字)之后立即键入空格。然而,根据具体情况,空间可能看起来很尴尬。什么更有意义?大多数人做什么?
抱歉,如果之前有人问过这个问题;我没有看到它出现。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也这样做。
这样对我来说更有意义。在
function
关键字后面添加空格更具可读性(我对if
、while
等也这样做),并且不这样做是有道理的将其放在函数名称之后,因为通常调用它时不带空格。I do the same.
It makes more sense to me this way. It is more readable with the space after the
function
keyword (I do the same withif
,while
, etc) and it makes sense not to put it after the function name since you usually invoke it without a space.这是个人喜好的问题。毫无疑问,适当的间距确实有助于提高可读性,这始终是一个好主意。
不过,当涉及到 JavaScript 编码风格时,重要的是始终将起始大括号放在相同的位置(由于 自动分号插入)行不同于:
阅读更多:
It is matter of personal preference. No doubt proper spacing does aid to readability which is always a good idea.
The important thing though when it comes to JavaScript coding style, is to always put starting curly brace on the same (due to automatic semi-colon insertion) line unlike:
Read More:
我同意,在编写 Javascript 代码时,我希望我的代码尽可能具有可读性,并且我发现使用空格来分隔关键字可以让我更轻松地阅读。
I agree, when coding Javascript i want my code to be as readable as possible and I've gotten to find that having a whitespace to separate the the keyword ease the reading, for me.
我的建议:
如果
()
是函数调用运算符,不要在其前面放置空格。在所有其他情况下,请在其前面添加一个空格。这样,您可以更轻松地识别调用运算符。
My suggestion:
If the
()
is the function invocation operator, don't put a space before it. In all other cases do put a space before it.That way, you can identify invocation operators more easily.