Thread: Functions
View Single Post
Old July 11th, 2006, 06:04 PM
nzakas's Avatar
nzakas nzakas is offline
Wrox Author
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts

That's not entirely a correct statement. The rule is:

You cannot capture variables outside of function scope when using the Function constructor. You can create closures, like this:

var myFunc = new Function("var a=0; return function () { return a; }");
Technically, this function creates a function that is a closure (it captures the variable "a").

Generally, it's considered poor practice to use the Function constructor to create new functions, though technically it's not wrong.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)