15.3.2 Function as a constructor

2010-07-04

function new Function([[p1:mixed[, p2:mixed[, ...]],] body]) throws SyntaxError

Creates a new (regular) function using parameters p1 .. pn as the parameters for the new function (can have no parameters) and the last argument as the body for the new function (can be empty). The returned object is a regular function, like the one returned by the expression function(){}.

Code: (Meta Ecma)
function Function(){
var argCount = arguments.length;
var P = '';
if (argCount == 0) var body = '';
else if (argCount == 1) body = arguments[0];
else {
var firstArg = arguments[0];
var P = ToString(firstArg);
var k = 2;
while (k < argCount) {
var nextArg = arguments[k];
var P += ","+ToString(nextArg);
++k;
}
var body = arguments[k];
}
// ugh, todo: sort this crap out
body = ToString(body);
var fpl = tokenize(P);
fpl = evaluate(fpl);
if (!fpl) throw SyntaxError;
var fb = tokenize(body);
fp = evaluate(fb);
if (!fb) throw SyntaxError;
var strict = !!fb.strict;
if (strict) // throw any exceptions specified in 13.1 that apply...
// create new function object as per 13.2, using P, body, global and strict.
}

A prototype property is created for every function in case it is going to be used as a constructor.

Note that you can combine parameters, Function("a,b","c", "body...") is the same as Function("a,b,c","body...") or Function("a","b","c","body...").