contents   index   previous   next



Function()

syntax:

new Function(params[, ...], body)

where:

params - one or a list of parameters for the function.

 

 

 

body - the body of the function as a string.

 

return:

object - a new function object with the specified parameters and body that can later be executed just like any other function.

 

description:

The parameters passed to the function can be in one of two formats. All parameters are strings representing parameter names, although multiple parameter names can be grouped together with commas. These two options can be combined as well. For example, new Function("a", "b", "c", "return") is the same as new Function("a, b", "c", "return"). The body of the function is parsed just as any other function would be. If there is an error parsing either the parameter list or the function body, a runtime error is generated. If this function is later called as a constructor, then a new object is created whose internal _prototype property is equal to the prototype property of the new function object. Note that this function can also be called directly, without the new operator.

 

example:

// The following will create a new Function object

// and provide some properties

// through the prototype  property.

 

var myFunction = new Function("a", "b",

    "this.value = a + b");

var printFunction = new Function

    ("Screen.writeln(this.value)");

myFunction.prototype.print = printFunction;

 

var foo = new myFunction( 4, 5 );

foo.print();

 

// This code will print out the value "9",

// which was the value stored in foo when it was

// created with the myFunction constructor.

 


Function apply()