Using Functions as Data

As illustrated in the previous section, functions can be treated as data. Like any other data, functions express some type of value, whether it be string, numeric, or Boolean. This next section considers two more methods of creating functions. The first method uses the Function() constructor, and the second uses function literals.

Using the Function() Constructor

The Function() constructor looks like the new object or array constructor. It has this general format:

							var variableName=new Function("exp1", "exp2", "return exp3;"); 

Here, exp1 is a necessary first expression, exp2 is an optional second expression, and exp3 is an expression made up of exp1 and exp2. For example, the following script uses item as exp1 and ...

Get JavaScript Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.