Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Space before left parenthesis in anonymous function literals #55

Closed
siclait opened this issue Feb 28, 2013 · 15 comments
Closed

Space before left parenthesis in anonymous function literals #55

siclait opened this issue Feb 28, 2013 · 15 comments

Comments

@siclait
Copy link

siclait commented Feb 28, 2013

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

@ssorallen
Copy link
Contributor

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.

@hshoff
Copy link
Member

hshoff commented Feb 28, 2013

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.

@timofurrer
Copy link
Contributor

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. :)

@hshoff
Copy link
Member

hshoff commented Mar 7, 2013

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

@leizhao4
Copy link

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]()

@matbhz
Copy link

matbhz commented May 12, 2015

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.

@leizhao4
Copy link

@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.

@goatslacker
Copy link
Collaborator

+1 to that @leizhao4

@ljharb
Copy link
Collaborator

ljharb commented May 12, 2015

+1 as well, function [name]( ftw

@L2L2L
Copy link

L2L2L commented May 12, 2015

I agree, and always follow.

Sent from my iPhone

On May 12, 2015, at 1:35 PM, Lei Zhao notifications@github.com 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.

@hshoff hshoff reopened this May 12, 2015
@nathanbuchar
Copy link

👍 @leizhao4

@ma11hew28
Copy link

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

@ljharb
Copy link
Collaborator

ljharb commented Dec 23, 2015

This is addressed here.

@ljharb ljharb closed this as completed Dec 23, 2015
@nicoder
Copy link
Contributor

nicoder commented Apr 26, 2016

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

ljharb added a commit to ljharb/javascript that referenced this issue Apr 26, 2016
@ljharb
Copy link
Collaborator

ljharb commented Apr 26, 2016

@nicoder thanks, done in 1e1772b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests