Space before left parenthesis in anonymous function literals
See original GitHub issueFrom 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:
- Created 11 years ago
- Reactions:1
- Comments:15 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@matbhz I don’t think it’s a whole thing. The full expression syntax is
function [name]() {}
wherefunction
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 infunction () {}
clearly shows that the function has no name.The function name and
()
is a whole thing. But thefunction
keyword is outside of it.I vote for the Crockford style.
function
is a keyword which should be followed by a space. Just like inif ()
,for ()
andwhile ()
. I findfunction()
as ugly asif()
,for()
andwhile()
.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]()