Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

1 of 2
NullPoiиteя
  • 57.3k
  • 23
  • 130
  • 148

First one(function doSomething(x)) should be part of an object notation.

The second one(var doSomething = function(x){ alert(x);}) is simply creating an anonymous function and assigning it to a variable doSomething . So doSomething() will call the function.

you may want to What is a Function Declaration and Function Expression

A Function Declaration defines a named function variable without requiring variable assignment. Function Declarations occur as standalone constructs and cannot be nested within non-function blocks.

function foo() {
 return 3;
}

ECMA 5 (13.0) defines the syntax as
function Identifier ( FormalParameterListopt ) { FunctionBody }

in above condition the function name is visible within it’s scope and the scope of it’s parent (otherwise it would be unreachable)

and in function expression

A Function Expression defines a function as a part of a larger expression syntax (typically a variable assignment ). Functions defined via Functions Expressions can be named or anonymous. Function Expressions should not start with "function"

//anonymous function expression
var a = function() {
 return 3;
}
//named function expression
var a = function foo() {
 return 3;
}
//self invoking function expression
(function foo() {
 alert("hello!");
})();

ECMA 5 (13.0) defines the syntax as
function Identifieropt ( FormalParameterListopt ) { FunctionBody }

NullPoiиteя
  • 57.3k
  • 23
  • 130
  • 148

AltStyle によって変換されたページ (->オリジナル) /