eval

0 views
Skip to first unread message

Joenco

unread,
Dec 19, 2002, 6:14:17 PM12/19/02
to
Hello,

I would like to know what the use is of eval.
I understand it executes its string but one can also execute them without
eval. So why does one need it?

Thank you,


Janwillem Borleffs

unread,
Dec 19, 2002, 6:47:54 PM12/19/02
to

"Joenco" <Jo_e...@hotmail.com> schreef in bericht
news:dpsM9.61432$Ti2....@afrodite.telenet-ops.be...

Eval EVALuates a string expression to executable code and runs it. Example:

var test = "alert('Hi there!')";
eval(test); // Alerts 'Hi There!'

Another one:

var test = "1+1";
alert(test); // Alerts '1+1'
alert(eval(test)); // Alerts '2'

Because of the quotes that enclose the value of test, it becomes a string.
Eval executes the string's statement which results in the number 2.

The great strength of eval is that you can compose a code snippet from
seperate variable bits that would only be possible otherwise with static
chops of code.


HTH

Joenco

unread,
Dec 19, 2002, 7:10:53 PM12/19/02
to
> Eval EVALuates a string expression to executable code and runs it.
Example:
>
> var test = "alert('Hi there!')";
> eval(test); // Alerts 'Hi There!'
>
> Another one:
>
> var test = "1+1";
> alert(test); // Alerts '1+1'
> alert(eval(test)); // Alerts '2'
>
> Because of the quotes that enclose the value of test, it becomes a string.
> Eval executes the string's statement which results in the number 2.
>
> The great strength of eval is that you can compose a code snippet from
> seperate variable bits that would only be possible otherwise with static
> chops of code.

Hello,

I understand what you say. But I have not yet seen examples where the use of
eval is better than the static chops of code. Can you give me an example
where eval is really needed?

Thank you,


Douglas Crockford

unread,
Dec 19, 2002, 7:53:50 PM12/19/02
to
> > I would like to know what the use is of eval.
> > I understand it executes its string but one can also execute them
without
> > eval. So why does one need it?

> Eval EVALuates a string expression to executable code and runs it.


Example:
>
> var test = "alert('Hi there!')";
> eval(test); // Alerts 'Hi There!'
>
> Another one:
>
> var test = "1+1";
> alert(test); // Alerts '1+1'
> alert(eval(test)); // Alerts '2'
>
> Because of the quotes that enclose the value of test, it becomes a string.
> Eval executes the string's statement which results in the number 2.
>
> The great strength of eval is that you can compose a code snippet from
> seperate variable bits that would only be possible otherwise with static
> chops of code.

eval() is there primarily for the benefit of the browser, which uses it to
execute the scriptet strings in HTML attributes.

eval() is the most misused feature of JavaScript. eval is evil.

http://www.JSON.org


Joenco

unread,
Dec 19, 2002, 8:00:58 PM12/19/02
to
> >> var test = "1+1";
> >> alert(test); // Alerts '1+1'
> >> alert(eval(test)); // Alerts '2'
> >>

> Gary

Gary and Janwillem,

Yes I understand. But in the above example it is just a lot easier to write
alert(1+1); Of course I do understand this is only a snippet to teach me.

What I would like to see is a real life example. If anyone knows a html page
where eval is used can you point me to it please.

Thank you,


Richard Cornford

unread,
Dec 19, 2002, 8:53:03 PM12/19/02
to
Joenco wrote in message ...

>
>What I would like to see is a real life example. If anyone knows a html
page
>where eval is used can you point me to it please.


There are thousands (maybe millions) of web pages with scripts that use
eval. However, as you appear to have correctly surmised, there is almost
no _need_ to use eval in JavaScript (some would say 'no need'). So, of
those thousands of pages, you would be lucky to find two examples where
the use of eval was necessary (or even advisable).

Richard.


Thomas Mlynarczyk

unread,
Dec 20, 2002, 3:34:28 AM12/20/02
to
Also sprach Douglas Crockford:

> eval() is the most misused feature of JavaScript. eval is evil.

Just because something is being misused doesn't mean it's "evil". There are
bad websites. Does it mean the Internet is evil?

--
Thomas Mlynarczyk

http://members.surfeu.de/home/tomtomatic
ME VELLE CIVEM ESSE TOTIUS MUNDI NON UNIUS OPPIDI.


Richard Cornford

unread,
Dec 20, 2002, 7:22:05 AM12/20/02
to
Gary News wrote in message ...
<snip>
>Jan said the same thing already. "Eval executes the string's
statements..."
>
>That is a powerful feature. You couldn't compose a sequence of
>code, using variables, and then have it executed as a command
>unless you could first use string concatenation to build the code
>chunk, then use the eval() function to EVALuate the chunk.
<snip>

Unless the composed sequence of code represented a function body and the
Function constructor was used to turn the string into a working
function.

Example:

function MouseTrail(frame){
this.frame = frame;
var varExpName = '';
while(frame != self){
frame = frame.parent;
varExpName += 'parent.';
}
MouseTrail.instances[
(this.index = MouseTrail.instances.length)] = this;
this.varName = varExpName+'MouseTrail.instances['+this.index+']';

. . .

this.frame.document.onmousemove =
new Function('a', ('return '+this.varName+'.mMove(a);'));
}

Alternatively, if not a function body, the composed sequence of code
could also be executed using setTimeout/setInterval. Although there is
no reason for using setTimeout over eval unless timing is needed.

Example:

MouseTrail.prototype.startS = function(){
this.timer = this.frame.setTimeout((this.varName+
'.startS();'), MouseTrail.rate);
this.train.doPos();
return;
}

Richard.


Richard Cornford

unread,
Dec 20, 2002, 9:20:43 AM12/20/02
to
Joenco wrote in message ...
<snip>

>What I would like to see is a real life example. If anyone knows
>a html page where eval is used can you point me to it please.


I thought of a real life (well, sort of) example of the need to use
eval. JavaScript 1.3 introduced a method on the Function.prototype
called 'apply' (which means - mid Netscape 4.n) but Microsoft did not
include it until IE 5 (maybe even 5.5) so a script that needed to use
'apply' could not be run on IE 4. One solution to the problem is to
determine whether a JavaScript environment supports 'apply' and if not
provide a suitable 'apply' emulation.

'apply' allows you to apply a function in the context of any object. It
takes two parameters, the first is the object to which you want the
function to apply and the second is an array of parameters for the
function. A full emulation of the JavaScript 'apply' function would have
to be able to cope with any number of parameters and to do that eval is
required.

This code provides the full emulation of the 'apply' function for
environments that do not support it already (within the limitations
imposed by using switch and delete). It is an amalgamation of code by
Douglas Crockford, Yep and myself (Douglas was in no way responsible for
the contents of the 'default' clause in the switch statement. He opposes
the use of eval completely and has his own 'apply' emulation at <URL:
http://www.crockford.com/javascript/remedial.html >)

if ((typeof Function != 'undefined')&&
(typeof Function.prototype != 'undefined')&&
(typeof Function.apply != 'function')) {
Function.prototype.apply = function(o, a){
var r, fn = '____apply'
while(typeof o[fn] != 'undefined') fn += fn;
o[fn] = this;
switch(((typeof a == 'object')&&(a.length))||0){
case 0:
r = o[fn]();
break;
case 1:
r = o[fn](a[0]);
break;
case 2:
r = o[fn](a[0], a[1]);
break;
case 3:
r = o[fn](a[0], a[1], a[2]);
break;
case 4:
r = o[fn](a[0], a[1], a[2], a[3]);
break;
default:
for(var ii=0, st=''; ii<a.length;ii++){
if(ii!=0) st += ',';
st += 'a[' + ii +']';
}
r = eval('o.'+fn+'('+st+');');
break;
}
delete o[fn];
return r;
};
}

The problem with citing a full emulation of the 'apply' function as a
'real life' example of a situation where eval is needed is that in 'real
life' you don't need a _full_ emulation of 'apply' at all. Internally
the JavaScript 1.3 apply function must be able to cope with an Array of
parameters of any length but an 'apply' emulation would only be used in
conjunction with specific scripts. It would always be possible to know
the maximum number of parameters that any function that would be used
with 'apply' would expect to receive and thus it is only necessary to
emulate 'apply' to the degree that the script would actually need to use
it (so no need for eval).

In the script that prompted my need to emulate 'apply', none of the
function that are called using 'apply' have more than 3 parameters and
none of them have return values. I also know that there is no need to
test the temporary property name ' ____apply' as I know that it will not
clash with existing names in the objects and there is no need to delete
that property between invocations. So this is the 'apply' emulation that
I actually use to fix IE 4:

if ((typeof Function != 'undefined')&&
(typeof Function.prototype != 'undefined')&&
(typeof Function.apply != 'function')) {
Function.prototype.apply = function(o, a){
o.____apply = this;
switch(((typeof a == 'object')&&(a.length))||0){
case 0:
o.____apply();
break;
case 1:
o.____apply(a[0]);
break;
case 2:
o.____apply(a[0], a[1]);
break;
default:
o.____apply(a[0], a[1], a[2]);
break;
}
};
}

It does exactly the job required and with no need for eval. The 'full'
emulation of 'apply' is more of an intellectual exercise than a
practical function and there isn't a high demand for using 'apply' in a
browser environment anyway.

Richard.


Dr John Stockton

unread,
Dec 20, 2002, 8:18:38 AM12/20/02
to
JRS: In article <eZtM9.61503$Ti2....@afrodite.telenet-ops.be>, seen in
news:comp.lang.javascript, Joenco <Jo_e...@hotmail.com> posted at Fri,
20 Dec 2002 01:00:58 :-

<URL:http://www.merlyn.demon.co.uk/js-demos.htm#Ev> uses it to test
user-supplied code. Do not use this page off the Web repeatedly; if you
want to re-use the scheme, copy the relevant parts. Underneath, #FZ &
#FP also require eval().

<URL:http://www.merlyn.demon.co.uk/js-astro.htm#Hor> also uses it, so
that one may type a simple numeric expression, such as 29028*0.3048, in
the input controls.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MSIE 4 ©
<URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

Reply all
Reply to author
Forward
0 new messages