Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save lizlongnc/6055aa487a30de94e98a to your computer and use it in GitHub Desktop.

Select an option

Save lizlongnc/6055aa487a30de94e98a to your computer and use it in GitHub Desktop.
JS function statement vs function expression
function expression
function
optional name
parameters
wrapped in ()
zero or more names
separated by a comma
body
wrapped in {}
zero or more statements
Produces an instance of a functon object
Function objects are first class
may be passed as an argument to a function
may be returned from a function
may be assigned to a variable
may be stored in an object or array
Function objects inherit from Function.prototype, so functions can have methods
A variable declared anywhere within a function is visible everywhere within the function.
function statement - same as function expression except the name is mandatory
function
mandatory name
parameters
wrapped in ()
zero or more names
separated by commas
body
wrapped in {}
zero or more statements
The function statement is a short-hand for a var statement with a function value:
function foo () {}
expands to
var foo = function foo () {};
which further expands to
var foo = undefined;
foo = function foo () {};
The assignment of the function is also hoisted. So function statements should be declared at the top level of its parent function if in a function.
In the standards function statements are called function declarations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment