[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

new Function

Jay Braun

9/30/2015 6:28:00 PM

I am having some trouble experimenting with 'new Function()' as an alternative to 'eval()'.

In my current application, a server-side C program produces, upon request from the client, a JavaScript file that begins:

function funcName(arg1, arg2, arg3) {
//and has many statements
}

which is executed on the client side with:

filerequest.onreadystatechange = function() {
if (filerequest.readyState==4 && filerequest.status==200) {
var e = eval(filerequest.responseText);
funcName(a1, a2, a3);
}
}

The function is called/executed successfully because the server-side file contains the entire function.

Simple on-line examples of 'new Function()' show the "new" function being constructed in-line by some JavaScript code. That's not what is occurring here, and my attempts to apply the examples have failed. In particular, the browser console indicates that funcName() is not recognized.

Do I need to change the server-side file to use 'new Function()'? And in the above example, how must I alter the client-side code that invokes the generated function? I am free to change either or both of these.

Jay
3 Answers

Evertjan.

9/30/2015 7:16:00 PM

0

Jay Braun <lyngwyst@gmail.com> wrote on 30 Sep 2015 in
comp.lang.javascript:

> I am having some trouble experimenting with 'new Function()' as an
> alternative to 'eval()'.

Is it?

How so?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Ben Bacarisse

9/30/2015 8:13:00 PM

0

Jay Braun <lyngwyst@gmail.com> writes:

> I am having some trouble experimenting with 'new Function()' as an
> alternative to 'eval()'.

OK, but unless this is just a learning exercise, why are you changing
code that works for code that does not yet work? Using new Function and
then calling the resulting function is not any safer than calling eval,
if that is your reason.

<snip>
> Simple on-line examples of 'new Function()' show the "new" function
> being constructed in-line by some JavaScript code. That's not what is
> occurring here, and my attempts to apply the examples have failed.

Well something is probably wrong with the "new Function" call, but how
can anyone possibly help if you don't show the code?

> In particular, the browser console indicates that funcName() is not
> recognized.

This indicates a significant misunderstanding. The result of the new
Function call is a function itself -- if it gets a name at all, it will
be because you store the result in variable.

--
Ben.

Jay Braun

10/1/2015 3:31:00 AM

0

I should have done more "homework" before posting this. My apologies.

Thank you,
Jay