By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

eliminating eval

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.