eliminating eval

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cp

    #1

    eliminating eval

    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="javas cript">
    // <![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="showDat a('half-orc', 'int')">
    &nbsp;
    </body>
    </html>

    --
    cp
  • Lasse Reichstein Nielsen

    #2
    Re: eliminating eval

    cp <cpryce@nospam. pryce.net> writes:
    [color=blue]
    > 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?[/color]

    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
    [color=blue]
    > var gRaces = new Array();[/color]
    ....[color=blue]
    > gRaces[ "half-elf" ]= { str:8, dex:10, con:8, 'int':8, wis:8,
    > cha:8 };[/color]

    Half-elves don't have a racial bonus to dex. :P
    [color=blue]
    > function showData(race, ab) {
    > alert( eval( 'gRaces[race].' + ab ) );[/color]

    Just as you write gRaces[race] instead of "gRaces."+r ace, 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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • cp

      #3
      Re: eliminating eval

      In article <y8sq6qca.fsf@h otpop.com>, Lasse Reichstein Nielsen
      <lrn@hotpop.com > wrote:
      [color=blue]
      > If you use XHTML, you should declare it somewhere. Otherwise,
      > CDATA-sections probably makes no difference
      >[/color]

      Thanks, I did (and do). I omitted the declaration for brevity.
      [color=blue][color=green]
      > > var gRaces = new Array();[/color]
      > ...[color=green]
      > > gRaces[ "half-elf" ]= { str:8, dex:10, con:8, 'int':8, wis:8,
      > > cha:8 };[/color]
      >
      > Half-elves don't have a racial bonus to dex. :P[/color]

      House rules? Kidding. Cut & Paste mistake
      [color=blue][color=green]
      > > function showData(race, ab) {
      > > alert( eval( 'gRaces[race].' + ab ) );[/color]
      >
      > Just as you write gRaces[race] instead of "gRaces."+r ace, you can use
      > square-bracket notation for ab:
      >
      > alert(gRaces[race][ab])[/color]

      Thanks for that.

      --
      cp

      Comment

      • Jim Ley

        #4
        Re: eliminating eval

        On Fri, 02 Jan 2004 14:58:05 -0600, cp <cpryce@nospam. pryce.net>
        wrote:
        [color=blue]
        >In article <y8sq6qca.fsf@h otpop.com>, Lasse Reichstein Nielsen
        ><lrn@hotpop.co m> wrote:
        >[color=green]
        >> If you use XHTML, you should declare it somewhere. Otherwise,
        >> CDATA-sections probably makes no difference
        >>[/color]
        >
        >Thanks, I did (and do). I omitted the declaration for brevity.[/color]

        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.javas cript FAQ - http://jibbering.com/faq/

        Comment

        Working...