3

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}

Is there any applicable reason why one would declare functions with vars instead of just regular function definitions, especially when dealing with closure.

Obviously this probably does not make much sense until i demonstrate, so i shall!

NOTE: I am using require.js to make this point obvious

Example A: How i normally do things

define(function() {

    function foo(x) {
        return x + 42;
    }

    function bar(y) {
        return y + foo(y);
    }

    var MyObject = function(config) {
        // some sweet stuff
    }

    MyObject.prototype = {
        myFun: function(x) {
            return bar(x)
        }
    }

    return MyObject;
})

Example B: Ways i see it

define(function() {

    var foo = function(x) {
        return x + 42;
    }

    var bar = function(y) {
        return y + foo(y);
    }

    var MyObject = function(config) {
        // some sweet stuff
    }

    MyObject.prototype = {
        myFun: function(x) {
            return bar(x)
        }
    }

    return MyObject;
})

I assume there must be some difference between the two, maybe... :)

Thank you for your time and effort!


EDIT: Tried to ask the question in a more sensible way!

Community
  • 1
  • 1
ThePrimeagen
  • 4,462
  • 4
  • 31
  • 44
  • Perhaps, but mine is more in a closure situation where you would always have it defined first (it could be the same, and if is, sorry!) – ThePrimeagen Jan 09 '13 at 21:17
  • 1
    @TomaszNurkiewicz This is a different question - That one asks "what is the difference", this asks "Is there a reason to use one vs. the other" – Jeff Jan 09 '13 at 21:18
  • 1
    @Jeff: I'd say the difference is the reason :-P – gen_Eric Jan 09 '13 at 21:20
  • Even if you prefer method B, you should at least say `var foo = function foo(x) { ... }` so that the function shows up with the name `foo` in a stack trace. – Raymond Chen Jan 09 '13 at 21:22
  • All functions are closures in JavaScript, so it does not really make a difference. – Felix Kling Jan 09 '13 at 21:22
  • If i am not mistaken there is an error in IE when doing the anonymous function naming – ThePrimeagen Jan 09 '13 at 21:24
  • 1
    @RocketHazmat No, the difference isn't the reason, but it may imply the reason. That implication may be non-trivial however. The fact that the reason may be reasoned from the difference makes no difference to the reason for the difference in questions, which is that reasoning is hard. When it comes down to it that's the whole reason SO exists in the first place. – Jeff Jan 09 '13 at 21:24
  • @Michael: Yep, older IE versions create duplicate functions if you use named function expressions. – Felix Kling Jan 09 '13 at 21:25
  • 3
    @Jeff i am pretty sure my brain just divided by 0 – ThePrimeagen Jan 09 '13 at 21:25
  • @Jeff: `the reason may be reasoned from the difference makes no difference to the reason for the difference`. OW, my brain! – gen_Eric Jan 09 '13 at 21:26
  • I disagree with the exact duplicate, but fine, what ever. – ThePrimeagen Jan 09 '13 at 21:28

1 Answers1

0

There is not really a gain. The Function Declarations you have in Example A get changed to function expressions and hoisted to the top of the closure so they look the same as what you have defined in your first example once the var declarations in Example B are hoisted.

The two forms you have are explicit assignment of function expressions and function declarations. Function declarations get changed into function expressions and the declaration itself is pulled up the top of the function (hoisting).

Read more here: http://elegantcode.com/2011/03/24/basic-javascript-part-12-function-hoisting/

Joshua Enfield
  • 17,642
  • 10
  • 51
  • 98