1

There are some conventions when it comes to using brackets in JavaScript, but do they actually get treated differently when the brackets are used to invoke.

Is fn () different to fn() in any way, except to the human reader?

Further, is there a difference between (fn)() and fn(), and if yes which of these to would fn () resemble?


For this question fn can be any function function fn() {} or var fn = function () {}


EDIT : This question isn't "Will they have the same result?", it is about if/how JavaScript compilers treat them.

Community
  • 1
  • 1
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • 2
    Have you actually tried these in an interpreter? – millimoose Nov 24 '12 at 02:34
  • @millimoose They will all work, that isn't what I'm asking. – Paul S. Nov 24 '12 at 02:36
  • Well, do they work any differently? (Another hint is the word "convention".) – millimoose Nov 24 '12 at 02:36
  • @millimoose `fn()` and `(1,fn)()` [**are different**](http://stackoverflow.com/a/9107367/1615483) but work the same. – Paul S. Nov 24 '12 at 02:44
  • 2
    Which is the opposite situation of the one you're asking about. Why should it imply anything significant? There's pretty much an infinite amount of expressions that produce any given value. But in your question, the syntactic differences between your examples are ones that **for any other expression** would have no semantical implications, and in fact certainly don't seem to make a difference when evaluated. What you're asking is tantamount to asking about the difference between `1+1`, `1 + 1`, and `(1) + (1)` – millimoose Nov 24 '12 at 02:47
  • @alex23 thanks and you seem to be knowledgeable about the spec, but this question isn't about defining `fn`. I'm surprised I haven't seen any answers about opening parenthesis on the next line or the fact `;` are optional yet, I think people got too caught up by my saying the function could have been an expression. – Paul S. Nov 24 '12 at 03:08

2 Answers2

3

No difference, fn () = fn() = (fn)() but function fn() {} != var fn = function() {} because of how hoisting works.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 2
    @alex23: There _is_ a substantial difference though. When you do `function foo(){}`, the fact is that `foo` will be available even before declaring the function because of how hoisting (not scoping) works. If you use `var foo = function(){}`, the variable `foo` will be `undefined` until the assignment is parsed, and only at that point you can use the function. The assumption that a function declaration implies that it's attached to the global scope is false; it's attached to the very top of the closest scope (function). – elclanrs Nov 24 '12 at 02:59
  • @alex23: I may agree with you on some points but I think you're wrong about this. I understand that in JS every object (or instance of an object) has a prototype that may or may not be extended but it doesn't change the behavior of how variables are scoped AFAIK... – elclanrs Nov 24 '12 at 03:19
  • It is bad practice to use arguments.callee, if youre on a modern browser use a named function expression and it can refer to itself by name. It is even disallowed in strict iirc – Paul S. Nov 24 '12 at 12:44
2

No, fn(), fn (), (fn)(), and (fn) () all mean the same thing.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
melpomene
  • 84,125
  • 8
  • 85
  • 148