How Do You Create a Function?
Problem
You want to know how many ways there are to create a function.
Solution
There are three ways to create a function, using a constructor, declaration, or expression.
The Code
Listing 12-1. Creating a Function
//function constructor
var fun1 = new Function('name', 'return name;');
fun1('Jessica');
//function declaration
function myFun(name){
var greeting = 'Hello ' + name;
return greeting;
}
myFun('Danny');
//function expression
var fun3 = function(name) {
return name;
}
fun3('Mami');
How It Works
There are ...