56 lines
1.0 KiB
Plaintext
56 lines
1.0 KiB
Plaintext
Because semicolons at the ends of statements are optional, starting function call arguments on a separate line makes the code confusing. It could lead to errors and most likely _will_ lead to questions for maintainers.
|
|
|
|
|
|
What was the initial intent of the developer?
|
|
|
|
. Define a function and then execute some unrelated code inside a closure ?
|
|
. Pass the second function as a parameter to the first one ?
|
|
|
|
The first option will be the one chosen by the JavaScript interpreter.
|
|
|
|
|
|
By extension, and to improve readability, any kind of function call argument should not start on new line.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
var fn = function () {
|
|
//...
|
|
}
|
|
|
|
(function () { // Noncompliant
|
|
//...
|
|
})();
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
Either
|
|
|
|
|
|
----
|
|
// define a function
|
|
var fn = function () {
|
|
//...
|
|
}; // <-- semicolon added
|
|
|
|
// then execute some code inside a closure
|
|
(function () {
|
|
//...
|
|
})();
|
|
----
|
|
|
|
Or
|
|
|
|
|
|
----
|
|
var fn = function () {
|
|
//...
|
|
}(function () { // <-- start function call arguments on same line
|
|
//...
|
|
})();
|
|
----
|
|
|