I’m coming across these two ways of declaring functions in Javascript.
One is an assignment expression that to declares foo to be whatever the function returns, i.e.
var foo = function(){
//do something
};
And the other way of declaring a function seems to make it a property of a larger object:
foo: function() { //do something }
I’m assuming you would use the second form when you needed to access that function in an object context, i.e.:
myobject.foo();
What is the proper name for the second form?
The following:
the right hand side is a FunctionExpression, it is different to a FunctionDeclaration in that the function isn’t created until the code is executed, which is after function declarations have been processed and variable instatiation has occurred.
It is not a function declaration.
that is also a function expression, to put it in the same form as the first:
and it too is only created when the code is executed. There is no practical difference between the two above, use whatever seems best.
Edit
Oh, and in a function expression, the name is optional (and generally recommended against because of issues with IE and named function expressions).