Javascript: Space before left parenthesis in anonymous function literals

Created on 28 Feb 2013  Â·  15Comments  Â·  Source: airbnb/javascript

From Crockford:

If a function literal is anonymous, there should be one space between the word function and the ( (left parenthesis). If the space is omited, then it can appear that the function's name is function, which is an incorrect reading.

div.onclick = function (e) {
  return false;
};

that = {
  method: function () {
    return this.datum;
  },
  datum: 0
};

Source: Code Conventions for the JavaScript Programming Language

Most helpful comment

@matbhz I don't think it's a whole thing. The full expression syntax is function [name]() {} where function is a keyword meaning we are going to declare a function, followed by a space and an optional name, and a parameter list, and the function body. The function name is optional but I don't think the space should go away. Having a space in function () {} clearly shows that the function has no name.

The function name and () is a whole thing. But the function keyword is outside of it.

All 15 comments

Since function is a reserved word I don't know who would actually mistake it for a function's name. It's an interesting syntax choice, but I don't feel compelled one way or the other.

I think the no space style function() {} makes it easier to identify anonymous functions at a glance.
With the space I immediately think that there's supposed to be something there.

Happy to leave this open and get more feedback.

I sympathize with @ssorallen and @hshoff views. In my opinion it's much easier to read and identify an anonymous functions without a space between the function keyword and the (). But maybe this is just my habit and there would be a very good reason to leave a space there. :)

Happy to reopen this if folks want to continue the discussion.

I vote for the Crockford style. function is a keyword which should be followed by a space. Just like in if (), for () and while (). I find function() as ugly as if(), for() and while().

Also I find it easier to identify an anonymous function by using the space, which clearly shows that the function has no name. var functionName = function [optionalName]()

I feel like function and () should be together because they are part of a same statement, such as a mathematics function f(x).

The space in the middle of if () suggest that something extra is going to be needed: they are different things which must in cooperation, whereas function() with no space is the whole thing by itself.

@matbhz I don't think it's a whole thing. The full expression syntax is function [name]() {} where function is a keyword meaning we are going to declare a function, followed by a space and an optional name, and a parameter list, and the function body. The function name is optional but I don't think the space should go away. Having a space in function () {} clearly shows that the function has no name.

The function name and () is a whole thing. But the function keyword is outside of it.

+1 to that @leizhao4

+1 as well, function [name]( ftw

I agree, and always follow.

Sent from my iPhone

On May 12, 2015, at 1:35 PM, Lei Zhao [email protected] wrote:

I vote for the Crockford style. function is a keyword which should be followed by a space. Just like in if (), for () and while (). I find function() as ugly as if(), for() and while().

—
Reply to this email directly or view it on GitHub.

:+1: @leizhao4

@leizhao4 you are a FUCKING GENIUS!!! https://youtu.be/tuhPPOXnyKo

This is addressed here.

should the ES5 examples be updated then ? (they currently use function())

@nicoder thanks, done in 1e1772b340702e55d0933997d5af733afb8c7584

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xgqfrms-GitHub picture xgqfrms-GitHub  Â·  3Comments

brendanvinson picture brendanvinson  Â·  4Comments

stephenkingsley picture stephenkingsley  Â·  3Comments

kozhevnikov picture kozhevnikov  Â·  3Comments

felixsanz picture felixsanz  Â·  3Comments