Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
Is there any applicable reason why one would declare functions with var
s 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!