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

Return to Answer

Copy edited. (its = possessive, it's = "it is" or "it has". See for example <http://www.wikihow.com/Use-Its-and-It%27s>.)
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134

FirstThe 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, doSomething. So doSomething() will call the function.

youYou may want to What isknow what a Function Declarationfunction declaration and Function Expressionfunction expression is.

A Function Declarationfunction declaration defines a named function variable without requiring variable assignment. Function Declarationsdeclarations 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 }

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

andAnd in a function expression

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

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

ECMA 5 (13.0) defines the syntax as

function Identifieropt ( FormalParameterListopt ) { FunctionBody }

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 }

The 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 know what a function declaration and function expression is.

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 its scope and the scope of its parent (otherwise it would be unreachable).

And in a 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 }

Source Link
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 }

lang-js

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