What is the difference between these two function declarations in JavaScript?

Possible duplicate:
JavaScript: var functionName = function () {} vs function functionName () {}

In JavaScript, we can say:

function a() {};

Or we could say

var a = function() {};

Can someone explain to me exactly how they differ, which, if any, are preferable and under what circumstances can each of them be used?

Any links or external readings will be highly appreciated.

+5
source share
4 answers

One is a function declaration, and one is a function expression.

http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

+7
source

First

function a() {};

FunctionDeclaration FunctionExpression. , FunctionDeclaration FunctionExpression, , .

# 1)

0,function a() {} //FunctionExpression

# 2)

(function a() {} ()); //FunctionExpression

№ 3)

var b = function a() {}; //FunctionExpression

# 4)

foo(function a(){}); //FunctionExpression

var a = function() {};

FunctionExpression .

1)

2)

:

http://sweatte.wordpress.com/syntax/javascript-idioms-and-gotchas/

http://www.ecma-international.org/publications/standards/Ecma-262.htm

+2

one - , - , . - javascript-.

0

0
source

All Articles