1

Editing someone else's code, I ran across a pattern I had not previously seen:

var functionName = function functionName(){};

and sometime later, to call the function, using this jQuery

$(functionName);

Now, before I change it to the standard function functionName(){} called with functionName();, is there any reason to do it the other way?

EDIT: Updated to reflect the use of jQuery in the function call. I oversimplified the example. (Oops! Sorry!)

Martin Burch
  • 2,184
  • 1
  • 23
  • 53
  • 3
    It's easier to create aliases of the function, but for the most part is quite trivial. – BenM Mar 26 '13 at 16:47
  • 1
    `functionName` will not call a function. You need `()` (or `new`, `apply`, `setTimeout`, etc) for a function to be called. – Quentin Mar 26 '13 at 16:50
  • Ah, @Quentin, you're absolutely right. The call was wrapped in jQuery: `$(codeAddress);`. Didn't realize that was important. I'll update the question. – Martin Burch Mar 26 '13 at 16:53
  • 2
    http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname might shed some light as to why the original dev used a variable stored anonymous function over a function call. – scrappedcola Mar 26 '13 at 16:55
  • Thanks @scrappedcola, this question is beginning to look like a dupe of that question, and a bit of an anti-pattern as well with the jQuery. – Martin Burch Mar 26 '13 at 16:57

5 Answers5

2
var workerFn = function someDefaultFn() {};

if ( lots of logic) {
  workerFn = function specialFn() {};
}

//// later on

workerFn();

So now we have flexibility as to what exactly is invoked. Sort of a poor-man's polymorphism.In your example we'd be passing the workerfn to JQuery to be invoked, so same possibility for flexibility.

djna
  • 52,574
  • 11
  • 70
  • 109
1

The only technical reasons for using a function expression would be to avoid hoisting of the function and/or being able to use another internal name to refer to the function itself.

These are the only differences between function expressions and function declarations and it depends on the context whether they are relevant at all.

Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
0

I'd say it's a bug because functionName; will not do anything. Or is it a typo in your question?

laktak
  • 47,916
  • 15
  • 112
  • 150
0

functionName;

would not call the function. This is just a reference to the function. Calling the function needs the (). So if we have

var functionName = function test(){ alert("0");};

this

functionName;

does not call it. Actually this does not do anything at all (no-op). This is same as

var x;
x;

Only this

functionName()

calls it.

Maybe the functionName; is used for something else. We can tell only if we have context.

Nivas
  • 17,354
  • 4
  • 57
  • 75
-1

[EDIT]
You can find var functionName = function(){}; and make as many instance this way var second = functionName;
It's helpful only if you want to have few variables to call the same function, but with a different param...

But in your case, functionName will contain only that the function functionName() {}; will return.

You propably have something like this in the function content:

var functionName = function test(param) {
    [...]
    return function(otherParam) { 
        //do something
    };
}
JoDev
  • 6,018
  • 1
  • 20
  • 33
  • Could you elaborate on your answer? Every function is an object and you can create any number of instances from a constructor function, no matter how it was defined. – Felix Kling Mar 26 '13 at 16:51
  • Copies of (a reference to) a function are not instances of that function (which are created with `var instance = new functionName(args);` – Quentin Mar 26 '13 at 16:54
  • `function foo(){}; var bar = foo;` works fine as well. That is no reason to use a function expression. – Felix Kling Mar 26 '13 at 16:56
  • Am I still wrong? Sorry for previous post, sometimes, my minds gone away... ;) – JoDev Mar 26 '13 at 16:56
  • Your answer gets more and more confusing. What are you trying to express with the code snippet? – Felix Kling Mar 26 '13 at 17:02
  • Sorry for confusion. I'd tried to explain why the given Martin Burch's code is wrong, and how to make it works. (I think I'm gonna sleep... ;) ) – JoDev Mar 26 '13 at 17:13