Possible Duplicate:
What is the difference between a function expression vs declaration in JavaScript?
This JavaScript syntax I haven’t seen till now, what does it do really?
What is the difference between the following two ways of writing a function? I’ve seen both used, but I’m not sure which one is ‘correct’.
function init() {
}
init: function() {
},
And what are the advantages to writing it the second way?
Function declaration
Function expression
The primary differences have to do with
Variable Hoistingin JavaScript. You can read more here: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting and http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/By your example, I believe you are also interested in defining
anonymous functionsinobject literals. Here is an example:This would be used like so:
In object literals different properties of the object are defined with a
key: valuesyntax and use commas to separate them.I would recommend going through this free series on JavaScript http://learn.appendto.com/lessons, it will answer a lot of these questions for you and give you a solid base to becoming a JS developer.