473,408 Members | 1,858 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,408 software developers and data experts.

Why eval(string) is not needed

// Look ma! no evals!
function eval2 (string, noReturn) {
return Function((!noReturn ? "return " : "")+string)(); }

var aR = eval2("/x/");
var Yo = eval2("{a:1, l:1, e:1, r:1, t:1}");
var a = eval2("['Hi', 'Mom']");
var aPri = eval2("a.join(' ').replace(aR,'u');");
var foo = eval2("var f='';for (var i in Yo) f+=i; return(f)",true);
eval2 (foo+"('"+aPri+"')");
Csaba Gabor from Vienna :)

Apr 1 '06 #1
3 1231
Csaba Gabor wrote:
// Look ma! no evals!
function eval2 (string, noReturn) {
return Function((!noReturn ? "return " : "")+string)(); }

var aR = eval2("/x/");
var Yo = eval2("{a:1, l:1, e:1, r:1, t:1}");
var a = eval2("['Hi', 'Mom']");
var aPri = eval2("a.join(' ').replace(aR,'u');");
var foo = eval2("var f='';for (var i in Yo) f+=i; return(f)",true);
eval2 (foo+"('"+aPri+"')");


The Function constructor is just an alias for eval. It is equally as evil.

var aR = /x/;
var Yo = {a: 1, l: 1, e: 1, r: 1, t: 1};
var a = ['Hi', 'Mom'];
var aPri = a.join(' ').replace(aR, 'u');
var foo = function () {
var f = '';
for (var i in Yo) {
f += i;
}
return f;
}();
foo(aPri);

http://javascript.crockford.com
Apr 1 '06 #2
"Csaba Gabor" <da*****@gmail.com> writes:
// Look ma! no evals!
function eval2 (string, noReturn) {
return Function((!noReturn ? "return " : "")+string)(); }


And merry first of April to you too :)

And a case where your eval2 fails to work like eval:

var x = "suckered!";
function foo() {
var x = 42;
alert(eval2("x"));
}
foo();

Good luck!
/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 1 '06 #3
VK

Csaba Gabor wrote:
// Look ma! no evals!
function eval2 (string, noReturn) {
return Function((!noReturn ? "return " : "")+string)(); }

var aR = eval2("/x/");
var Yo = eval2("{a:1, l:1, e:1, r:1, t:1}");
var a = eval2("['Hi', 'Mom']");
var aPri = eval2("a.join(' ').replace(aR,'u');");
var foo = eval2("var f='';for (var i in Yo) f+=i; return(f)",true);
eval2 (foo+"('"+aPri+"')");
Csaba Gabor from Vienna :)


eval('alert(eval(\'2+2\')/eval(\'2\'))'); // 2

Instead of three absolutely necessary eval's I could make it with just
one (others are inside the string so doesn't count :-)

Apr 1 '06 #4

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

Similar topics

12
by: lawrence | last post by:
I have a string which I want to send to eval(). How can I test it ahead of time to make sure it is valid code? I don't want to send it to eval and get parse errors. I want to do something like...
3
by: Grayson | last post by:
I have a need for an "Eval" function and found the perfect sample The problem is it doesn't like "IsDate" being fed into it. Any Ideas? seems like a missing reference, but I can't figure it out......
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
4
by: Wilson | last post by:
Hello, How can i eval a string expression like String abc = ??"dt.Rows.Columns.ToString() + dt.Rows.Columns.ToString()" Thanks Wilson
5
by: R | last post by:
Hi All, I'm using eval and arrays in foreach and have trouble with adding elements to them - I'm talking about the '' operator. My code is: // creates arrays with the names of columns in...
11
by: C.W.Holeman II | last post by:
I am looking for an example using the object argument to eval(). http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:eval -- C.W.Holeman II | cwhii@Julian5Locals.com-5...
9
by: Bob M | last post by:
If I define an expression or equation, how can I retrieve that expression as string literal? I want to do this so that I could avoid repetitive typing (or copy/paste/change) the same thing at two...
33
by: cesco | last post by:
Hi, say I have a string like the following: s1 = 'hi_cat_bye_dog' and I want to replace the even '_' with ':' and the odd '_' with ',' so that I get a new string like the following: s2 =...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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
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.