Revision f5a071d1-c0c0-4371-a8d2-c78ea4b9df28 - Stack Overflow
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 <br/>
> function Identifier ( FormalParameterList<sub>opt</sub> ) { 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 <br/>
> function Identifier<sub>opt</sub> ( FormalParameterList<sub>opt</sub> ) { FunctionBody }