The ways to define a javascript function

1. Declarative Function

/*
The syntax:

function functionname(param1, param2,param3,...,paramn){
      finction statements
}

or 

var functionname = function (param1, param2,param3,...,paramn){
      finction statements
}

Example:*/

function sayHi(toWhom){
alert("hi " + toWhom);
}
sayHi("World");

var sayHi = function (toWhom){
alert("hi " + toWhom);
}
sayHi("World");

 

2. Anonymous Function

/*Syntax:
var variable= new Function("param1", "param2", ...,"paramn", "function body");

Example:*/

var func = new Function("x", "y",  "alert('x + y  is ' + (x + y))");
func(1, 2);

 3. Function Literal or Function Expression

function funcObject(x, y, op){
alert(op(x, y));
}

funcObject(1, 2,  function(x, y){return x + y;});
 

 

你可能感兴趣的:(JavaScript)