473,408 Members | 1,861 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.

eliminating eval

cp
If I have the following functions, is there a way to alter showData()
to get at the value of the array without resorting to an eval?
<html>
<head>
<title>Test</title>
<script type="text/javascript" language="javascript">
// <![CDATA[
var gRaces = new Array();
gRaces[ "elf" ]= { str:8, dex:10, con:6, 'int':8, wis:8,
cha:8 };
gRaces[ "half-elf" ]= { str:8, dex:10, con:8, 'int':8, wis:8,
cha:8 };
gRaces[ "half-orc" ]= { str:10, dex:8, con:8, 'int':6, wis:8,
cha:6 };

function showData(race, ab) {
alert( eval( 'gRaces[race].' + ab ) );
}
// ]]>
</script>
</head>
<body onload="showData('half-orc', 'int')">
&nbsp;
</body>
</html>

--
cp
Jul 20 '05 #1
3 1219
cp <cp****@nospam.pryce.net> writes:
If I have the following functions, is there a way to alter showData()
to get at the value of the array without resorting to an eval?
Yes. (I say that before looking at the code, because I am a firm believer
in the abolishment of unneeded evals)
If you use XHTML, you should declare it somewhere. Otherwise,
CDATA-sections probably makes no difference
var gRaces = new Array(); .... gRaces[ "half-elf" ]= { str:8, dex:10, con:8, 'int':8, wis:8,
cha:8 };
Half-elves don't have a racial bonus to dex. :P
function showData(race, ab) {
alert( eval( 'gRaces[race].' + ab ) );


Just as you write gRaces[race] instead of "gRaces."+race, you can use
square-bracket notation for ab:

alert(gRaces[race][ab])

See
<URL:http://jibbering.com/faq/#FAQ4_39>
and
<URL:http://jibbering.com/faq/#FAQ4_40>

/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.'
Jul 20 '05 #2
cp
In article <y8**********@hotpop.com>, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
If you use XHTML, you should declare it somewhere. Otherwise,
CDATA-sections probably makes no difference


Thanks, I did (and do). I omitted the declaration for brevity.
var gRaces = new Array();

...
gRaces[ "half-elf" ]= { str:8, dex:10, con:8, 'int':8, wis:8,
cha:8 };


Half-elves don't have a racial bonus to dex. :P


House rules? Kidding. Cut & Paste mistake
function showData(race, ab) {
alert( eval( 'gRaces[race].' + ab ) );


Just as you write gRaces[race] instead of "gRaces."+race, you can use
square-bracket notation for ab:

alert(gRaces[race][ab])


Thanks for that.

--
cp
Jul 20 '05 #3
On Fri, 02 Jan 2004 14:58:05 -0600, cp <cp****@nospam.pryce.net>
wrote:
In article <y8**********@hotpop.com>, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
If you use XHTML, you should declare it somewhere. Otherwise,
CDATA-sections probably makes no difference


Thanks, I did (and do). I omitted the declaration for brevity.


There are certainly no XHTML user agents that want the language
attribute on the script element.

There's also precious few XHTML user agents at all, probably best not
to use it.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #4

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

Similar topics

7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
11
by: sneill | last post by:
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...
2
by: VM | last post by:
How can I eliminate or hide the first column in a datarow? This is the column that doesn't have any data; it just points to the record the user's in (the column with the horizontal arrow). Thanks,...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
3
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
8
by: Rasmus Kromann-Larsen | last post by:
The With Conundrum I'm currently writing a master thesis on (preparations for) static analysis of JavaScript, and after investigating the with statement, it only even more evident to me that the...
5
jamesd0142
by: jamesd0142 | last post by:
My manager and I where looking at some complex code to eliminate duplicate records in a database table. then it hit me how its done easily... so i thought i'd share it... In English:...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
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?
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
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
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
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...

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.