question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Space before left parenthesis in anonymous function literals

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:1
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
leizhao4commented, May 12, 2015

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

5reactions
leizhao4commented, May 12, 2015

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

space-before-function-parentheses - ESLint
This rule aims to enforce consistent spacing before function parentheses and as such, will warn whenever whitespace doesn't match the preferences specified.
Read more >
Explain the encapsulated anonymous function syntax
Those extra parenthesis creates extra anonymous functions between global namespace and anonymous function that contains the code.
Read more >
Whitespace · Styleguide JavaScript
Place one (1) space before the opening parenthesis in control statements ( if , while etc.). Place no space between the argument list...
Read more >
Optional Braces - Scala 3 - EPFL
Indentation Rules · Optional Braces · Optional Braces Around Template Bodies · Optional Braces for Method Arguments · Spaces vs Tabs · Indentation...
Read more >
EditorConfig properties for JavaScript: Spaces - ReSharper
EditorConfig properties for JavaScript: Spaces ... leave_multiple : Leave multiple extra spaces ... Before parentheses of anonymous method.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found