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) { } 
5
  • 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: stackoverflow.com/questions/1013385/… and stackoverflow.com/questions/5403121/…. Also typehints such as int don't exist in JS (that's invalid too). Commented Oct 10, 2012 at 18:34
  • It is inside of var x = declare("...", { <<right here>> }); What does that syntax achieve? can I not use regular syntax inside of an object like that? Commented Oct 10, 2012 at 18:37
  • 1
    So { <<right here>> } 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. Commented Oct 10, 2012 at 18:38
  • So myFunction is a variable? Why not myFunction = function(variable)? Commented Oct 10, 2012 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 developer.mozilla.org/en-US/docs/JavaScript/Guide/…. Commented Oct 10, 2012 at 18:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

what does the ":" operator do?
Two examples of defining a method myobject.mymethod = functionName or function() {...}; var myobject= { mymethod : function(params) { // ...do something } };
: 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.