I ran a script through JSLint and it picked out a specific issue with parenthesis placement.
I had written:
(function(){})();
And it was suggested to use:
(function(){}());
I'm curious as to what bugs or issues this particular change fixes. I would assume that because JSLint picked it out as an issue, there must be an issue for someone.
Expanded forms:
(
function (p) {
...code...
}
)(param); //parameters after the parens
-vs-
(
function (p) {
...code...
}(param) //parameters within the parens
);
To establish a sense of closure, you might do one or more of the following: Conclude by linking the last paragraph to the first, perhaps by reiterating a word or phrase you used at the beginning. Conclude with a sentence composed mainly of one-syllable words.
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.
To use a closure, define a function inside another function and expose it. To expose a function, return it or pass it to another function. The inner function will have access to the variables in the outer function scope, even after the outer function has returned.
The specific issue JSLint is trying to fix relates to a lack of closing ;
which can cause a bug where a function is interpreted as an argument:
(function A( arg ){
// stuff
})
(function B(){
...
});
Is perfectly valid, B
is passed to A
as arg
. However, this is often not the intended case as often these are meant to be self-executing and the trailing ()
were forgotten. The suggested syntax removes any confusion that you may have accidentally forgotten to execute your function as intended.
For what it's worth, I pretty much always use the first syntax as well; habit.
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