Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28765 > unrolled thread

new Function()

Started byJay Braun <lyngwyst@gmail.com>
First post2015-09-30 11:28 -0700
Last post2015-09-30 20:30 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  new Function() Jay Braun <lyngwyst@gmail.com> - 2015-09-30 11:28 -0700
    Re: new Function() "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-30 21:15 +0200
    Re: new Function() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-30 21:12 +0100
      Re: new Function() Jay Braun <lyngwyst@gmail.com> - 2015-09-30 20:30 -0700

#28765 — new Function()

FromJay Braun <lyngwyst@gmail.com>
Date2015-09-30 11:28 -0700
Subjectnew Function()
Message-ID<a52a763b-4769-4ca6-8f82-9785514df782@googlegroups.com>
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

[toc] | [next] | [standalone]


#28767

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-09-30 21:15 +0200
Message-ID<XnsA525D84374353eejj99@194.109.6.166>
In reply to#28765
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)

[toc] | [prev] | [next] | [standalone]


#28768

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-09-30 21:12 +0100
Message-ID<87h9mb64zp.fsf@bsb.me.uk>
In reply to#28765
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.

[toc] | [prev] | [next] | [standalone]


#28769

FromJay Braun <lyngwyst@gmail.com>
Date2015-09-30 20:30 -0700
Message-ID<c1826b07-3724-408d-b8e6-4e3838fea5a4@googlegroups.com>
In reply to#28768
I should have done more "homework" before posting this.  My apologies.

Thank you,
Jay

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web