Janwillem Borleffs wrote:

HopfZ wrote:
How can I create a function nF such that for example

var sum = nF('a','b','return a+b');

and

var sum = new Function('a','b','return a+b');

do the same?

function fn(args, body) {

return new Function(args, body);

}

var f = fn(['a','b'],'return a+b');

alert(f(1,2,3));

JW

Thanks.

I wanted to make functions R and D that work as below.

/* t.

R('a_+b_*c_') returns function(a_,b_,c_){return (a_+b_*c_);}

R('x,y,z->(x+y)/z') returns function(x,y,z){return ((x+y)/z);}

D('if(b_) print(a_)') returns function(a_,b_){if(b_) print(a_)};

D('x,y->if(y) print(x)') returns function(x,y){if(y) print(x)};

R meaning Return, and D meaning Do, a_,b_,c_ meaning first, second and

third argument.

*/

function D(s){

if(/[abc]\_/.test(s)){

return new Function(['a_','b_','c_'],s);

}

var t = s.split('->');

return new Function(t[0].split(','),t[1]);

}

function R(s){

if(/[abc]\_/.test(s)){

return new Function(['a_','b_','c_'],'return ('+s+');');

}

var t = s.split('->');

return new Function(t[0].split(','),'return ('+t[1]+');');

}

I was stuck because I didn't know that (new Function('a','b','return

a+b')) can be alternatively written as (new Function(['a','b'],'return

a+b')).