Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any difference between f() and (f())?

Tags:

javascript

Any difference between

var myfunc = (function () { return function () { ... } }());

and

var myfunc = function () { return function () { ... } }();

Is it just a matter of style or is there more to the surrounding () in the first form?

like image 855
user2526241 Avatar asked Jun 27 '13 01:06

user2526241


People also ask

What is the difference between f and f?

Relationship between f, f' and f'' To our common sense, when f is always greater than o, then the function is always above x-axis, and when f is always less than 0, f is always below the x-axis.

Is f x and f x the same?

For instance, in statistics, F(x) and f(x) mean two different functions. F(x) represents the cumulative distribution function, or cdf in short, of a random variable as opposed to f(x) which represents the probability density function, or pdf, of the continuous random variable.

What is capital f in calculus?

The capital Latin letter F is used in calculus to represent the anti-derivative of a function f. Typically, the symbol appears in an expression like this: ∫abf(x)dx=F(b)−F(a)


2 Answers

Nope. Or at least not in your example.

The outer parens only matter when the function keyword would be the first token in a statement.

// cool
var foo = function(){}();
var foo = (function(){}());

// also cool
(function(){}());

// not cool, syntax error
// parsed as function statement, expects function name which is missing
function(){}();

// also not cool, syntax error
// declares a function, but can't be executed immediately
function foo(){}();

When function is the first token in a statement, it's a function declaration (think named function), which behaves slightly differently than function in all other contexts. That leading paren forces the parses to treat it like a function expression (think anonymous function) instead, which allows immediate execution.

See: What is the difference between a function expression vs declaration in JavaScript?

If you start the line or statement with something else, like variable declaration, it technically doesn't matter at all.

like image 173
Alex Wayne Avatar answered Oct 09 '22 09:10

Alex Wayne


No difference, though Crockford advises the use of the former, to ascertain that it's being treated as a function expression.

For more info, read these:

  • Immediately-Invoked Function Expression (IIFE)
  • Named function expressions demystified
like image 27
Joseph Silber Avatar answered Oct 09 '22 09:10

Joseph Silber