The first:
function functionName (){
//Code goes here;
}
Is a function declaration. It defines a function
object in the context it's written in.
Notice: this doesn't have to be the global
context and it doesn't
say anything about the value of this
inside it when it's invoked. More about scopes in JavaScript.
Second note: in most style guides functions are declared with a capitalized name only if it's a constructor.
The second:
var myFuncCollection = {};
myFuncCollection.functionName = function () {
//Code goes here;
};
notice: don't use the new Object()
syntax, it's considered bad practice to use new
with anything other then function constructors. Use the literal form instead (as above).
Is a simple assignment of a function expression to a property of an Object
.
Again the same notice should be stated: this says nothing about the value of this
when it's invoked.
this
in JavaScript is given a value when the function
object is invoked, see here for details.
Of course, placing a function
on an Object
help avoiding naming collisions with other variables/function declarations in the same context, but this could be a local context of a function, and not necessarily the global
context.
Other then these differences, from the language point of view, there's no difference whatsoever about using a bunch of function declarations or an Object with bunch of methods on it.
From a design point of view, putting methods on an Object
allows you to group and/or encapsulate logic to a specific object that should contain it. This is the part of the meaning of the Object Oriented Programming paradigm.
It's also good to do that when you wish to export or simply pass all these functions to another separate module.
And that's about it (: