473,414 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Problem with eval

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.

Apr 17 '06 #1
8 1634
wrote:
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.

Try adding 'alert(x);' just before the eval and you should see your problem
pretty quickly. x is the string "displayId([object Object])" which is
obviously rubbish. You cannot put an arbitrary object into a string for use
by eval.

How to overcome this? Well, the code you posted doesn't show any reason for
using eval at all. So just use:

function CallDisplay()
{
nodeobj=new mynode();
displayId(nodeobj);
}

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.
Apr 17 '06 #2
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

Apr 17 '06 #3
wrote:
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

Is there any reason you need to pass in the name of the function? Why not
just pass the function itself in directly?

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

CallDisplay(displayId);

Apr 17 '06 #4
Oh that's great. Thanks for your help. I passed the displayId within
the single quote. Now got it.

Apr 17 '06 #5
sa***********@sify.com 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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 17 '06 #6
JRS: In article <Xn*************************@127.0.0.1>, dated Mon, 17
Apr 2006 09:37:28 remote, seen in news:comp.lang.javascript, Duncan
Booth <du**********@invalid.invalid> posted :

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>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 17 '06 #7
Dr John Stockton said the following on 4/17/2006 2:49 PM:
JRS: In article <Xn*************************@127.0.0.1>, dated Mon, 17
Apr 2006 09:37:28 remote, seen in news:comp.lang.javascript, Duncan
Booth <du**********@invalid.invalid> posted :
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.


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Pedantic people should be careful about how they word things.
Apr 18 '06 #8
Dr John Stockton wrote:
JRS: In article <Xn*************************@127.0.0.1>, dated Mon,
17 Apr 2006 09:37:28 remote, seen in news:comp.lang.javascript, Duncan
Booth <du**********@invalid.invalid> posted :

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's why I said 'very few cases' rather than 'no cases'. If you have a
situation where you want to evaluate a string entered from outside your
code as an expression, then you may need to use eval or its equivalent, but
that isn't a common situation.

Most questions about eval seem to involve trying to lookup a value which
could be accessed far more easily by just using the object directly, or by
using the name to access a property of another object. The problem is that
eval is a hammer, and as such you can use it to drive a screw home, but you
really shouldn't.
Apr 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
1
by: Darren Clark | last post by:
I need to be able to send through 2 peices of infomraiton in the commandArgument property of a image button... <asp:imagebutton CommandArgument='<%# DataBinder.Eval(Container,"DataItem."+...
6
by: Roebie | last post by:
Hi everyone, I'm having some weird problem with evaluating the continue statement. Within a for loop I'm trying to evaluate a string (generated somewhere earlier) which basically has the continue...
2
by: tshad | last post by:
This is related to my other Hiding datalistitems problem that I can't seem to solve. I have tried different methods which all seem to work only partially. I decided to try to use a User...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
4
by: Antonio Carpentieri | last post by:
Hi all, in my previous post I've wrong typed some tems.. this is the corrected post. in a aspx page I have a repeater like this: <asp:repeater id=repeaterResults runat="server"...
1
by: Frank | last post by:
I am having a problem with using a repeater control. I have no idea what I am doing wrong. It compiles but nothing displays. Can anyone help? Here is the code: ASPX File: <%@ Page...
18
by: len.hartley | last post by:
Hi, I am trying to pop-up a window when the user clicks on an image. The problem is that when the user clicks on the image and the window pops up OK, but the window underneath also proceeds to...
2
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import namespace="System.Web.UI.HtmlControls" %> <%@ import...
0
by: uncensored | last post by:
Hello everyone, I'm fairly new at .Net and I have a repeater inside a repeater problem. I will attach my code to this message but basically what I am able to tell when I run my page it tells me...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.