Section 8.9. The Function() Constructor


8.9. The Function() Constructor

As explained earlier, functions are usually defined using the function keyword, either in the form of a function definition statement or a function literal expression. Functions can also be defined with the Function() constructor. Using the Function() constructor is typically harder than using a function literal, and so this technique is not commonly used. Here is an example that creates a function with the Function() constructor:

 var f = new Function("x", "y", "return x*y;"); 

This line of code creates a new function that is more or less equivalent to a function defined with the familiar syntax:

 function f(x, y) { return x*y; } 

The Function() constructor expects any number of string arguments. The last argument is the body of the function; it can contain arbitrary JavaScript statements, separated from each other by semicolons. All other arguments to the constructor are strings that specify the names of the parameters to the function being defined. If you are defining a function that takes no arguments, you simply pass a single stringthe function bodyto the constructor.

Notice that the Function() constructor is not passed any argument that specifies a name for the function it creates. Like function literals, the Function() constructor creates anonymous functions.

There are a few points that are important to understand about the Function() constructor:

  • The Function() constructor allows JavaScript code to be dynamically created and compiled at runtime. It is like the global eval() function (see Part III) in this way.

  • The Function() constructor parses the function body and creates a new function object each time it is called. If the call to the constructor appears within a loop or within a frequently called function, this process can be inefficient. By contrast, a function literal or nested function that appears within a loop or function is not recompiled each time it is encountered. Nor is a different function object created each time a function literal is encountered. (Although, as noted earlier, a new closure may be required to capture differences in the lexical scope in which the function is defined.)

  • A last, very important point about the Function() constructor is that the functions it creates do not use lexical scoping; instead, they are always compiled as if they were top-level functions, as the following code demonstrates:

     var y = "global"; function constructFunction() {     var y = "local";     return new Function("return y");  // Does not capture the local scope! } // This line displays "global" because the function returned by the // Function() constructor does not use the local scope. Had a function // literal been used instead, this line would have displayed "local". alert(constructFunction()());  // Displays "global" 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net