JavaScript 函数后的空格

发布于 2025-01-05 23:13:12 字数 459 浏览 5 评论 0 原文

我知道空格在 JavaScript 中是无关紧要的,但我对样式很好奇。

定义如下函数时:

function Foo(a, b, c) {}

我不会在函数名称后面添加空格。但是,如果我创建一个函数作为表达式:

Bar(function (a, b, c) {
    // do something
})

或者

{
    Foo: function (a, b, c) {
        // do something
    }
}

我发现自己自然地键入了一个空格。我认为这是因为我已经训练自己在函数关键字(或一般关键字)之后立即键入空格。然而,根据具体情况,空间可能看起来很尴尬。什么更有意义?大多数人做什么?

抱歉,如果之前有人问过这个问题;我没有看到它出现。

I know that white space is irrelevant in JavaScript, but I am curious about style.

When defining a function like:

function Foo(a, b, c) {}

I do not put a space after the function name. But, if I am creating a function as an expression:

Bar(function (a, b, c) {
    // do something
})

or

{
    Foo: function (a, b, c) {
        // do something
    }
}

I find myself naturally typing a space. I think this is because I've trained myself to type a space immediately after the function keyword (or keywords in general). However, depending on the context, space can look awkward. What makes more sense? what do most people do?

Sorry if this has been asked before; I didn't see it come up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那伤。 2025-01-12 23:13:12

我也这样做。

function () { ... }
function name() { ... }

这样对我来说更有意义。在 function 关键字后面添加空格更具可读性(我对 ifwhile 等也这样做),并且不这样做是有道理的将其放在函数名称之后,因为通常调用它时不带空格。

I do the same.

function () { ... }
function name() { ... }

It makes more sense to me this way. It is more readable with the space after the function keyword (I do the same with if, while, etc) and it makes sense not to put it after the function name since you usually invoke it without a space.

池木 2025-01-12 23:13:12

这是个人喜好的问题。毫无疑问,适当的间距确实有助于提高可读性,这始终是一个好主意。

不过,当涉及到 JavaScript 编码风格时,重要的是始终将起始大括号放在相同的位置(由于 自动分号插入)行不同于:

function myFunc() 
{
    return
    {
        name: 'Jane'
    };
}

var f = myFunc();
console.log(f); // undefined

阅读更多:

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:

function myFunc() 
{
    return
    {
        name: 'Jane'
    };
}

var f = myFunc();
console.log(f); // undefined

Read More:

白色秋天 2025-01-12 23:13:12

我同意,在编写 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.

迷荒 2025-01-12 23:13:12

我的建议:

如果 () 是函数调用运算符,不要在其前面放置空格。在所有其他情况下,在其前面添加一个空格。

function f ( x ) {
    // ...
}

var g = function ( y ) {
    // ...
};

f( 1 );
g( 2 );

这样,您可以更轻松地识别调用运算符。

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.

function f ( x ) {
    // ...
}

var g = function ( y ) {
    // ...
};

f( 1 );
g( 2 );

That way, you can identify invocation operators more easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文