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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With