Thursday, October 15, 2015

JavaScript Function Definition

You can define a function with a:
  • function (definition) expression
  • function (declaration) statement

How to Define/Declare Functions

  • Function Expression
    function [funcName]([arg1, ... [, argn]]){
    • A function expression produces a function object
    • A function expression is like an object literal: in this case it produces an object that inherits from Function.Prototype
  • Function Statement
    function funcName([arg1, ... [, argn]]){
    • A function statement expands into a var statement which declare a variable and assigns a function value to it.
    • Unlike ordinary var statement, both pieces of function statement are hoisted up to the start of script or function