473,472 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sorry... another question about eval()

I have read a number of posts on the use of eval() in Javascript, and I
agree that its use is questionable. But it does beg the following
question:

"How arbitrary does a string need to be before the use of eval() is
required to execute it?"

Given the following code, I'm able to evaluate/execute most expressions
like: "a.b.c.d()"

var aPart = sExpression.split(".");
var oContext = window;
var i = 0;
var iSize = aPath.length - 1;

while (i < iSize && (oContext = oContext[aPart[i++]])) { }

// execute the function
oContext[aPart[i].replace(/\(\)/, "")]();

But given a more complex expression, is the use of eval() justified?
For example, what about: "a.b(c.d())"

Any thoughts?

Steve

Aug 12 '05 #1
11 1837
sn****@mxlogic.com said the following on 8/12/2005 4:31 PM:
I have read a number of posts on the use of eval() in Javascript, and I
agree that its use is questionable.
It's beyond questionable. 99% (probably more) of the uses of eval you
see in scripting is due directly to the incompetence of the person who
wrote it.
But it does beg the following question:

"How arbitrary does a string need to be before the use of eval() is
required to execute it?"


Arbitrary doesn't matter. What matters is whether you know the string,
and it's content, at runtime or not. eval's use is to execute code that
is unknown at runtime.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 12 '05 #2
The question posed in the original post seems more academic. The key
phrase is "arbitrary", possibly implying the string has unknown
complexity. Given this scenario, should eval() be used?

Aug 12 '05 #3
st********@gmail.com said the following on 8/12/2005 5:37 PM:
The question posed in the original post seems more academic.
What original post? The one you didn't bother to quote anything about?

<URL: http://jibbering.com/faq/#FAQ2_3 >

"When replying to a message on the group trim quotes of the preceding
messages to the minimum needed and add your comments below the pertinent
section of quoted material, as per FYI28/RFC1855 (never top post). "
The key phrase is "arbitrary", possibly implying the string has unknown
complexity.
How written words are taken is in the mind of the reader, not the
writer. It means that what you read and get out of something is not what
someone else may get from it.

This was the question:
<quote>
"How arbitrary does a string need to be before the use of eval() is
required to execute it?"
</quote>

And the answer remains the same:

<quote cite="answer">
Arbitrary doesn't matter. What matters is whether you know the string,
and it's content, at runtime or not. eval's use is to execute code that
is unknown at runtime.
</quote>
Given this scenario, should eval() be used?


Only if there is no other way to do it, and that depends directly on
what the code is. It is not a simple question with a simple answer as
you seem to be trying to make it.

But generally, the answer is no. Do NOT use eval until every, any and
all other remedies/attempts are exhausted.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 13 '05 #4
sn****@mxlogic.com writes:
"How arbitrary does a string need to be before the use of eval() is
required to execute it?"
If the string is known when you write the code (or if you build it
using code that is), then eval is *never* needed, and all it does is
to delay any syntax errors you might make to when eval is called,
instead of when the program is loaded.

So, I'm assuming that you have a string provided only at runtime.
Given the following code, I'm able to evaluate/execute most expressions
like: "a.b.c.d()"


However, that string looks like it is written specially for the
current page, since it knows about the runtime environment it is being
executed in. That, most likely, means that it was the author of the
page (or someone related) who wrote the string. In that case you can
trust the format and using "eval" on it isn't much different from
evaluating it as a <script src="thecode.js" ...> element.

If the string is *not* supplied by the same authority as the page,
then it is less likely to be based on the structure of the data
already available in the runtime environment, and more likely to be
expected in a restricted format.

If that format happens to be a subset of Javascript syntax and
has the same semantics as it does in Javascript, then you might
use eval. However, I would first check that the string does
have the expected format.

E.g., if the user must provide a fraction, e.g., "2/7", then I would
*check* the format, most lilely using a regular expression like
(/^\d+\/[1-9]\d*$/).

I might use eval after that, because I know what is going to happen and
that the eval will not do something unexpected, but it would be about as
simple to just capture the numbers with the regexp and do the evaluation
manually:
var match = /^(\d+)\/([1-9]\d*)$/.exec(string)
var frac = match[1] / match[2];
(This particular example actually showed eval being slightly faster,
but that's not something I'd worry about unless you are doing a *lot*
of conversions).
So, eval is never needed for strings provided when the script is
written, nor for strings generated by the program itself.

For trusted strings provided at runtime, eval can be reasonable.

For untrusted strings provided at runtime, you should first check
the format. The format is probably so simple that you don't need
eval if you can check it.

The danger of eval is that it hides errors by being more acceptig than
what is really needed for a given job, by turning syntax errors into
runtime errors, and increasing complecity by adding an extra level to
the programming (code *and* values that become code, at the same
time). Complexity makes maintenance harder.

The average web page scripter is not trained in computer science, and
extra complexity is an error waiting to happen. He should not use eval
at all.

The competent developer should consider whether eval really is the
simplest, and most maintainable, way of doing what is needed. It might
be, but if you need to ask in this group, we'll assume you are not
able to judge it yourself, and then you shouldn't use it. :)

/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.'
Aug 13 '05 #5
JRS: In article <AMudnZ2dnZ1zkm32nZ2dne-MYN-dnZ2dRVn-
yJ*****@comcast.com>, dated Fri, 12 Aug 2005 17:15:35, seen in
news:comp.lang.javascript, Randy Webb <Hi************@aol.com> posted :

Arbitrary doesn't matter. What matters is whether you know the string,
and it's content, at runtime or not. eval's use is to execute code that
is unknown at runtime.


That would be clever; but perhaps you mean code that cannot be known at
authoring time, and that does something that cannot be done by other
code knowable at authoring time.

--
© 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.
Aug 13 '05 #6
Dr John Stockton said the following on 8/13/2005 7:46 AM:
JRS: In article <AMudnZ2dnZ1zkm32nZ2dne-MYN-dnZ2dRVn-
yJ*****@comcast.com>, dated Fri, 12 Aug 2005 17:15:35, seen in
news:comp.lang.javascript, Randy Webb <Hi************@aol.com> posted :
Arbitrary doesn't matter. What matters is whether you know the string,
and it's content, at runtime or not. eval's use is to execute code that
is unknown at runtime.

That would be clever; but perhaps you mean code that cannot be known at
authoring time,


No, I meant what I wrote. Code unknown at runtime.
and that does something that cannot be done by other
code knowable at authoring time.


No, again, I meant what I wrote. Code unknown at runtime.

<input type="text" onchange="eval(this.value)">

Given that in a local (or web page) as an input to calculate formulas
that are known by the user but unknown by the author. Yes, it could be
done with "other code" that is knowable at authoring time. But it is
ingnorant at best to write code, other than eval, to accomplish it.

Given this expression (or one of your own imagination):

9*8/5-6*15+26-59+35.6-98/52

You could write code that would split it, determine precedence, and then
perform calculations. But why? That is eval's use: To execute code
unknown at runtime.

Now, if you are dynamically creating code, then it is still known at
runtime by the author and can be decided whether to eval it or not, but
it is still reasonably known at runtime.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 13 '05 #7
JRS: In article <08********************@comcast.com>, dated Sat, 13 Aug
2005 17:46:21, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Dr John Stockton said the following on 8/13/2005 7:46 AM:
JRS: In article <AMudnZ2dnZ1zkm32nZ2dne-MYN-dnZ2dRVn-
yJ*****@comcast.com>, dated Fri, 12 Aug 2005 17:15:35, seen in
news:comp.lang.javascript, Randy Webb <Hi************@aol.com> posted :
Arbitrary doesn't matter. What matters is whether you know the string,
and it's content, at runtime or not. eval's use is to execute code that
is unknown at runtime.

That would be clever; but perhaps you mean code that cannot be known at
authoring time,


No, I meant what I wrote. Code unknown at runtime.
and that does something that cannot be done by other
code knowable at authoring time.


No, again, I meant what I wrote. Code unknown at runtime.

<input type="text" onchange="eval(this.value)">

Given that in a local (or web page) as an input to calculate formulas
that are known by the user but unknown by the author. Yes, it could be
done with "other code" that is knowable at authoring time. But it is
ingnorant at best to write code, other than eval, to accomplish it.

Given this expression (or one of your own imagination):

9*8/5-6*15+26-59+35.6-98/52

You could write code that would split it, determine precedence, and then
perform calculations. But why? That is eval's use: To execute code
unknown at runtime.


That code is known at runtime. Not known at the start of runtime, not
known by the page author at any time, but known during runtime. One can
give a non-existent variable to eval, or one with a value that is
undefined, null, NaN; but not one whose value is unknown.

--
© 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.
Aug 14 '05 #8
Dr John Stockton said the following on 8/14/2005 9:30 AM:
JRS: In article <08********************@comcast.com>, dated Sat, 13 Aug
2005 17:46:21, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Dr John Stockton said the following on 8/13/2005 7:46 AM:
JRS: In article <AMudnZ2dnZ1zkm32nZ2dne-MYN-dnZ2dRVn-
yJ*****@comcast.com>, dated Fri, 12 Aug 2005 17:15:35, seen in
news:comp.lang.javascript, Randy Webb <Hi************@aol.com> posted :
Arbitrary doesn't matter. What matters is whether you know the string,
and it's content, at runtime or not. eval's use is to execute code that
is unknown at runtime.
That would be clever; but perhaps you mean code that cannot be known at
authoring time,
No, I meant what I wrote. Code unknown at runtime.

and that does something that cannot be done by other
code knowable at authoring time.


No, again, I meant what I wrote. Code unknown at runtime.

<input type="text" onchange="eval(this.value)">

Given that in a local (or web page) as an input to calculate formulas
that are known by the user but unknown by the author. Yes, it could be
done with "other code" that is knowable at authoring time. But it is
ingnorant at best to write code, other than eval, to accomplish it.

Given this expression (or one of your own imagination):

9*8/5-6*15+26-59+35.6-98/52

You could write code that would split it, determine precedence, and then
perform calculations. But why? That is eval's use: To execute code
unknown at runtime.

That code is known at runtime.


No, it would not be if it were user entered. Read what I wrote "known by
the user but unknown by the author". The only thing the authors knows is
that it *should* be a mathematical formula.
Not known at the start of runtime, not known by the page author at
any time, but known during runtime.
I see now, you are splitting hairs, ok.
One can give a non-existent variable to eval, or one with a value that is
undefined, null, NaN; but not one whose value is unknown.


Let me explain it a little better in a way that you may understand what
I meant then. Code that is unknown at the time of execution. Now, stop
splitting hairs, or is that the best you can do?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 14 '05 #9
Randy Webb <Hi************@aol.com> writes:
Let me explain it a little better in a way that you may understand
what I meant then. Code that is unknown at the time of execution.


Still not a good way of saying it. At time of execution, the
Javascript interpreter does know the code. What, I think, you
are trying to say is tat the code is unknown by the author,
i.e., unknown at the time the script is written.

/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.'
Aug 15 '05 #10
Lasse Reichstein Nielsen said the following on 8/14/2005 9:16 PM:
Randy Webb <Hi************@aol.com> writes:

Let me explain it a little better in a way that you may understand
what I meant then. Code that is unknown at the time of execution.

Still not a good way of saying it. At time of execution, the
Javascript interpreter does know the code. What, I think, you
are trying to say is tat the code is unknown by the author,
i.e., unknown at the time the script is written.


Unknown by the author at time of execution.

If it is dynamically created code, then it can be reasonably known by
the author at the time the script is written, but eval wouldn't be the
best way of executing it.

But since I very seldom use eval other than testing it's speed against
other methods, it's a moot point to me. Just don't use it and it's not a
problem :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Aug 15 '05 #11
Thank you John, Lasse for your constructive comments.

Steve

Aug 15 '05 #12

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

Similar topics

5
by: Brian Angliss | last post by:
I'm relatively new to scripting in JavaScript, so I'm not too surprised I'm having difficulty scripting up an animation effect for my personal site. What I'm trying to do is the following: When...
6
by: Frances Del Rio | last post by:
var myform= document.formPricing; var one = "price"; alert(one); var amount = myform.one.value; the alert prints name of element fine, however, on line var amount = myform.one.value;
1
by: Fraggle | last post by:
I have a repeater with controls added at run time. the <template> also contains a <asp:textbox that is made visible on some repeater elements. when I come to read the text info out it has...
0
by: Logan McKinley | last post by:
I have a datalist that takes two clicks to show the SelectedItem or UpdateItem. The first click seems to fire the event but does not seem to change the page, the second click doesn't fire the...
1
by: Chris | last post by:
Hey Okay using DataBinder.Eval in a repeater. My question is this - if the value is empy, eg the myDownloadFile is empty, how can i display different output than if it was populated. #...
10
by: Patrick Olurotimi Ige | last post by:
I have a checkbox and i want to input Char "Y" or "N" to the Table In C# we could use for example :- ptrTest.Value = chkYN.Checked ? "Y" : "N"; Whats the equivalent in VB.NET?
0
by: erin.sebastian | last post by:
Hello All, I have created a web page. At the top of the page there are 4 links; upon clicking on one of the links it brings up the left hand side menu, it's a repeater that loops through items...
9
by: peashoe | last post by:
I need to create a javascript that not only changes a picture, but also the link: here is an example of what I need www.myweddingfavors.com/ I'm working on this website and have it half done:...
7
by: Helpful person | last post by:
I am new to Javascript and have a fairly straightforward question. I am trying to use an image as a link to open a new page with the onmouseclick event. In general this seems to work fine with...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.