Problem with eval

S

santel_helvis

Hi,

Here is my code.

function CallDisplay()
{
nodeobj=new mynode();
x="displayId("+nodeobj+")";
eval(x);

}
function displayId(node)
{
alert(node.myid);
}
function mynode()
{
this.myid="aa";
}

In CallDisplay() I am getting the error as "Microsoft JScript
Compilation error ']' expected". Any one can suggest me how to
overcome this.
 
S

santel_helvis

Hi, Thanks for the reply.

Actually I will be passing the function name (displayId) in CallDisplay
function.

function CallDisplay(fnName)
{
nodeobj=new mynode();
x=fnName+"("+nodeobj+")";
eval(x);
}

I will be passing different names to this CallDisplay. So only I used
eval. Am I going wrong? I don't find the different way. Plz suggest
 
S

santel_helvis

Oh that's great. Thanks for your help. I passed the displayId within
the single quote. Now got it.
 
L

Lasse Reichstein Nielsen

(e-mail address removed) writes:

Please quote a little of the message you reply to.
Actually I will be passing the function name (displayId) in CallDisplay
function.

Why not pass the function itself. It's less fragile and more easily
readable.

function callDisplay(fn) {
var nodeobj = new MyNode():
fn(nodeobj);
}

If you *really* need to pass the function name, and you are certain
that a function is available as a variable of that name in the current
scope, then you should only eval as much as necessary:

function callDisplay(fnName) {
var nodeobj = new MyNode();
var fn = eval(fnName);
fn(nodeobj);
}

or the shorter:
function callDisplay(fnName) {
eval(fnName)(new MyNode());
}

If you want to create the call as a string (and there is no good
reason for that at all), you should make sure that the expression you
create is correct. Generally, only simple values can be converted to a
string representation of themselves by mere conversion to string
(undefined, null, numbers and booleans). Objects do not have a string
representation that preserves identity, and strings need quoting and
escaping. It's much easier to just embed a variable name:
function CallDisplay(fnName)
{
nodeobj=new mynode();
x=fnName+"("+nodeobj+")";

var x = fnName+"(nodeobj)";
eval(x);
}
I will be passing different names to this CallDisplay. So only I used
eval. Am I going wrong?

Most likely. Just pass the functions directly.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 17
Apr 2006 09:37:28 remote, seen in Duncan
Booth said:
You will find there are very few cases where you actually want to use eval.
Usually, if you think you want eval, you are mistaken and your code will be
cleaner and simpler avoiding it.

Except sometimes (treating the use of a new Function wrapper as morally
equivalent); I sometimes use eval for reading input controls.

That allows input by expression, so that where a distance in kilometres
is called for a user can enter a distance of 5 miles as 5*1.609 (or
something more complex to be exact), and 1/-0 can be used for -Infinity.

If the user chooses to put unreasonable code in, the consequences are
his problem, not mine.

Example in <URL:http://www.merlyn.demon.co.uk/$rnd.htm>.
 
R

Randy Webb

Dr John Stockton said the following on 4/17/2006 2:49 PM:
JRS: In article <[email protected]>, dated Mon, 17
Apr 2006 09:37:28 remote, seen in Duncan


Except sometimes (treating the use of a new Function wrapper as morally
equivalent); I sometimes use eval for reading input controls.

If you "sometimes use eval for reading input controls" then you are
misusing eval. Now, if you use eval to execute the value of that control
- that is different.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

eval(string) scope ? 3
eval() or What? 3
Problem with code 4
Problem with codewars. 5
problem with eval and JSON 16
Need Assistance With A Coding Problem 0
Linked list in C 4
Help needed with thank you message 5

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top