2

Possible Duplicate:
What is the difference between a function expression vs declaration in Javascript?

What is the difference between the two ways to declare a function in JavaScript?

myFunction : function(variable) 
{

}

or

function myFunction(variable)
{

}
Community
  • 1
  • 1
antonpug
  • 13,724
  • 28
  • 88
  • 129
  • 4
    The first snippet is invalid unless it is in the context of an object literal. If you want to know the difference between function expressions and declarations: http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip and http://stackoverflow.com/questions/5403121/whats-the-difference-between-function-foo-and-foo-function. Also typehints such as `int` don't exist in JS (that's invalid too). – Felix Kling Oct 10 '12 at 18:34
  • It is inside of var x = declare("...", { <> }); What does that syntax achieve? can I not use regular syntax inside of an object like that? – antonpug Oct 10 '12 at 18:37
  • 1
    So `{ <> }` is an object literal and `myFunction` is a property of it, holding a function as value. Still, in the first case you have a function expression and in the second one a function declaration and the differences between those two have been thoroughly discussed before. – Felix Kling Oct 10 '12 at 18:38
  • So myFunction is a variable? Why not myFunction = function(variable)? – antonpug Oct 10 '12 at 18:40
  • In the first case it's a property of some object, in the second case, yes, I guess one could say it's a variable. If you want to know how objects work, please have a look at https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects. – Felix Kling Oct 10 '12 at 18:41

2 Answers2

2

Your first code snippet is not valid - it only works within an object; example:

var object = {
    myFunction: function(variable) { }
};
// object.myFunction();


Basically there are two ways to define a function ins JavaScript:

function myFunction(variable) { }

var myFunction = function(variable) { };

The difference is: The first type of declaration uses the function statement and therefore allows you to use the function before it has been declared. Example:

console.log(myFunction());    // prints test
function myFunction(variable) { return "test"; }

Read more about it here.

This is not possible with the second type of function declaration, which assigns an anonymous function to a variable. The function can't be used before the variable has been declared.

Community
  • 1
  • 1
Aletheios
  • 3,960
  • 2
  • 33
  • 46
1

One is a method. The other a function.

Functions are defined

function myfunction() {..}

Methods are defined

myobject.mymethod = function() {...} ;

A method is a property of an object that points to / is a function of that object

Really it depends on how you structure your objects. Functions are usually used in global libraries that are non object specific while methods are tied to objects to execute specific pieces of functionality.

DRobertE
  • 3,478
  • 3
  • 26
  • 43
  • what does the ":" operator do? – antonpug Oct 10 '12 at 18:58
  • 1
    Two examples of defining a method myobject.mymethod = functionName or function() {...}; var myobject= { mymethod : function(params) { // ...do something } }; – DRobertE Oct 10 '12 at 19:00
  • : defines a key value pair... property : value where the property can be a method or variable and value can be a function declaration, definition, or function name which is just a pointer to the function or a value like 2 – DRobertE Oct 10 '12 at 19:05