eval

Discussion in 'Javascript' started by Stuart, Nov 5, 2003.

  1. Stuart

    Stuart Guest

    why won't the following work

    for(var i=0;i<pics;i++){
    eval('img'+i) = new Image(wth,hgt)
    eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    }

    basically I am trying to create a numer of placeholders with thier
    associated imgages!

    thanks in advance
    stuart
     
    Stuart, Nov 5, 2003
    #1
    1. Advertising

  2. > why won't the following work
    >
    > for(var i=0;i<pics;i++){
    > eval('img'+i) = new Image(wth,hgt)
    > eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    > }


    Where did you get this code?
     
    Douglas Crockford, Nov 5, 2003
    #2
    1. Advertising

  3. Stuart wrote:

    > why won't the following work


    Because it's evil[tm] eval(...). Besides, you are neither telling what you
    are consider working nor what `pics' aso. are. Nevertheless, despite its
    bad style, the below code does create Image objects and assigns a value to
    their `src' property, so I consider it working.

    > for(var i=0;i<pics;i++){
    > eval('img'+i) = new Image(wth,hgt)
    > eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    > }
    >
    > basically I am trying to create a numer of placeholders with thier
    > associated imgages!


    Provided that `pics' stores the number of Image objects to create, that
    `wth' and `hgt' are values for the width and height of *each* object and
    that the image resources to be accessed are available via the URLs
    http://www.mypics/X1.gif, where X is a number from 0 to pics - 1, use

    var myImages = new Object();
    for (var i = 0; i < pics; i++)
    {
    myImages['img' + i] = new Image(wth, hgt);
    myImages['img' + i].src = 'http://www.mypics/' + i + '1.gif';
    }

    instead.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 5, 2003
    #3
  4. > > > why won't the following work
    > > >
    > > > for(var i=0;i<pics;i++){
    > > > eval('img'+i) = new Image(wth,hgt)
    > > > eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    > > > }

    > >
    > > Where did you get this code?

    >
    > It is part of a bigger script that I am writting, the problem that I am
    > facing is that I cannot asign the src value to a constucted variable


    Who told you to use eval()?
     
    Douglas Crockford, Nov 5, 2003
    #4
  5. Stuart

    Lee Guest

    Stuart said:
    >
    >
    >Douglas Crockford <> wrote in message
    >news:55f44$3fa95a14$436563ac$...
    >> > why won't the following work
    >> >
    >> > for(var i=0;i<pics;i++){
    >> > eval('img'+i) = new Image(wth,hgt)
    >> > eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    >> > }

    >>
    >> Where did you get this code?

    >
    >It is part of a bigger script that I am writting, the problem that I am
    >facing is that I cannot asign the src value to a constucted variable
    >
    >I need to create a variable amount of placeholders and cache images such as
    >
    >img1 = new Image(100,200)
    >img1.src = 'someURL'
    >img2 = new Image(100,200)
    >img2.src = 'someURL'
    >img3 = new Image(100,200)
    >img3.src = 'someURL'
    >
    >I am trying to construct the placeholder through a loop as folows
    >
    >for(var i=1;i<pics;i++){
    > ( img + i ) = new Image(100,200)
    > (img + i).src = 'someURL'
    >}
    >
    >what I don't know is how to join img and i


    Whenever you find yourself using eval(), you've probably
    overlooked a simpler way to do something.

    var img=new Array();
    for(var i=0;i<pics;i++){
    img = new Image(100,200);
    img.src = 'someURL';
    }
     
    Lee, Nov 5, 2003
    #5
  6. Thomas 'PointedEars' Lahn wrote:

    > Stuart wrote:
    >> why won't the following work

    >
    > Because it's evil[tm] eval(...). Besides, you are neither telling what you
    > are consider working nor what `pics' aso. are. Nevertheless, despite its
    > bad style, the below code does create Image objects and assigns a value to
    > their `src' property, so I consider it working.
    >
    >> for(var i=0;i<pics;i++){
    >> eval('img'+i) = new Image(wth,hgt)
    >> eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    >> }


    Hm, having tested it in Mozilla 1.5b and IE 6.0 SP-1, I'm getting a script
    error because eval(...) isn't allowed as left-hand side expression (IE says
    explicitely: "Cannot assign to a result of a function" (or similar,
    translated it from German). Maybe I'm mixing up something here, but I
    remember ECMAScript implementations where it was possible to use it there.
    However, it's still bad style.


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 5, 2003
    #6
  7. Stuart

    Stuart Guest

    Douglas Crockford <> wrote in message
    news:55f44$3fa95a14$436563ac$...
    > > why won't the following work
    > >
    > > for(var i=0;i<pics;i++){
    > > eval('img'+i) = new Image(wth,hgt)
    > > eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    > > }

    >
    > Where did you get this code?


    It is part of a bigger script that I am writting, the problem that I am
    facing is that I cannot asign the src value to a constucted variable

    I need to create a variable amount of placeholders and cache images such as

    img1 = new Image(100,200)
    img1.src = 'someURL'
    img2 = new Image(100,200)
    img2.src = 'someURL'
    img3 = new Image(100,200)
    img3.src = 'someURL'

    I am trying to construct the placeholder through a loop as folows

    for(var i=1;i<pics;i++){
    ( img + i ) = new Image(100,200)
    (img + i).src = 'someURL'
    }

    what I don't know is how to join img and i
     
    Stuart, Nov 5, 2003
    #7
  8. > > Who told you to use eval()?
    > >

    > I am trying anything....including asking you!
    > Are you getting the jist of what I am trying to achieve?


    It is almost always extremely wrong to use eval(). Who is teaching you to
    program so badly?
     
    Douglas Crockford, Nov 5, 2003
    #8
  9. "Stuart" <> schreef in bericht
    news:bobmgv$95i$...
    >
    > I am trying to construct the placeholder through a loop as folows
    >
    > for(var i=1;i<pics;i++){
    > ( img + i ) = new Image(100,200)
    > (img + i).src = 'someURL'
    > }
    >
    > what I don't know is how to join img and i
    >


    Try it as follows:

    for(var i=1;i<pics;i++){
    window['img' + i] = new Image(100,200)
    window['img' + i].src = 'someURL'
    }


    JW
     
    Janwillem Borleffs, Nov 5, 2003
    #9
  10. Stuart

    Stuart Guest

    Douglas Crockford <> wrote in message
    news:8a03$3fa95f61$436563ac$...
    > > > > why won't the following work
    > > > >
    > > > > for(var i=0;i<pics;i++){
    > > > > eval('img'+i) = new Image(wth,hgt)
    > > > > eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    > > > > }
    > > >
    > > > Where did you get this code?

    > >
    > > It is part of a bigger script that I am writting, the problem that I am
    > > facing is that I cannot asign the src value to a constucted variable

    >
    > Who told you to use eval()?
    >

    I am trying anything....including asking you!
    Are you getting the jist of what I am trying to achieve?
     
    Stuart, Nov 5, 2003
    #10
  11. Stuart

    Stuart Guest

    Thomas 'PointedEars' Lahn <> wrote in message
    news:bobnnp$1cghhm$-berlin.de...
    > Thomas 'PointedEars' Lahn wrote:
    >
    > > Stuart wrote:
    > >> why won't the following work

    > >
    > > Because it's evil[tm] eval(...). Besides, you are neither telling what

    you
    > > are consider working nor what `pics' aso. are. Nevertheless, despite its
    > > bad style, the below code does create Image objects and assigns a value

    to
    > > their `src' property, so I consider it working.
    > >
    > >> for(var i=0;i<pics;i++){
    > >> eval('img'+i) = new Image(wth,hgt)
    > >> eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    > >> }

    >
    > Hm, having tested it in Mozilla 1.5b and IE 6.0 SP-1, I'm getting a script
    > error because eval(...) isn't allowed as left-hand side expression (IE

    says
    > explicitely: "Cannot assign to a result of a function" (or similar,
    > translated it from German). Maybe I'm mixing up something here, but I
    > remember ECMAScript implementations where it was possible to use it there.
    > However, it's still bad style.
    >

    Yes, I am getting these exact same errors,. However, I am pleased you
    understood what I am trying to do, May be it is "bad style" but I am new to
    javaScript, so may be you could point me in the way of "Good Style" and the
    correct way of scripting.
     
    Stuart, Nov 5, 2003
    #11
  12. Stuart wrote:

    [Repaired quoting, please read
    http://home.in.tum.de/~jain/software/oe-quotefix/]

    > Thomas 'PointedEars' Lahn <> wrote [...]
    >> Thomas 'PointedEars' Lahn wrote:
    >> [...] May be it is "bad style" but I am new to
    >> javaScript, so may be you could point me in the way of "Good Style" and the
    >> correct way of scripting.


    I already did, what part of it did you not understand?

    Maybe if you would shorten the quotes to what is required for following the
    discussion, which saves disk space and bandwidth, and eases reading, you
    were forced to read postings more thoroughly:

    http://www.albion.com/netiquette/corerules.html


    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 5, 2003
    #12
  13. Stuart wrote:

    > Thomas 'PointedEars' Lahn <> wrote [...]
    > > However, [eval(...) is] still bad style.

    >
    > [...] May be it is "bad style" but I am new to javaScript, so may be you
    > could point me in the way of "Good Style" and the correct way of
    > scripting.


    I already did, what part of it did you not understand?

    Maybe if you would shorten the quotes to what is required for following the
    discussion, which saves disk space and bandwidth, and eases reading, you
    were forced to read postings more thoroughly:

    http://www.albion.com/netiquette/corerules.html


    HTH

    PointedEars
     
    Thomas 'PointedEars' Lahn, Nov 5, 2003
    #13
  14. > > > > Who told you to use eval()?
    > > > >
    > > > I am trying anything....including asking you!
    > > > Are you getting the jist of what I am trying to achieve?

    > >
    > > It is almost always extremely wrong to use eval().
    > > Who is teaching you to program so badly?


    > And your input has been ?????


    My input is that you should file a lawsuit against whoever it was who taught you
    to program. I think you may be entitled to significant damages.
     
    Douglas Crockford, Nov 5, 2003
    #14
  15. Stuart wrote:

    > Thomas 'PointedEars' Lahn <> wrote [...]
    >> Stuart wrote:
    >> > [...] May be it is "bad style" but I am new to javaScript, so may be you
    >> > could point me in the way of "Good Style" and the correct way of
    >> > scripting.

    >>
    >> I already did, what part of it did you not understand?

    >
    > I see nothing worthwhile that you have sugested!


    You may want to re-read <news:bobmpr$1c3igv$-berlin.de>

    >> Maybe if you would shorten the quotes to what is required for following

    > the

    ^^
    Your newsreader is b0rken. Please read
    http://home.in.tum.de/~jain/software/oe-quotefix/
    and fix it.

    >> discussion, which saves disk space and bandwidth, and eases reading, you
    >> were forced to read postings more thoroughly:

    >
    > If you are so concerned about bandwidth, why have you posted a somewhat
    > pointless reply TWICE ??


    I did not. Please get informed about control messages like Cancel.
    And BTW, it would do you no harm to get informed about Usenet and
    the recommended behaviour in this discussion medium anyway before
    accusing people of spamming you that are actually trying to help
    you, despite a lack of minimum clue about the topic on your side.


    PointedEars, Score adjusted
     
    Thomas 'PointedEars' Lahn, Nov 5, 2003
    #15
  16. Stuart

    Stuart Guest

    Many thanks Lee.....works perfectly

    thanks again
    Stuart


    Lee <> wrote in message
    news:...
    > Stuart said:
    > >
    > >
    > >Douglas Crockford <> wrote in message
    > >news:55f44$3fa95a14$436563ac$...
    > >> > why won't the following work
    > >> >
    > >> > for(var i=0;i<pics;i++){
    > >> > eval('img'+i) = new Image(wth,hgt)
    > >> > eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'
    > >> > }
    > >>
    > >> Where did you get this code?

    > >
    > >It is part of a bigger script that I am writting, the problem that I am
    > >facing is that I cannot asign the src value to a constucted variable
    > >
    > >I need to create a variable amount of placeholders and cache images such

    as
    > >
    > >img1 = new Image(100,200)
    > >img1.src = 'someURL'
    > >img2 = new Image(100,200)
    > >img2.src = 'someURL'
    > >img3 = new Image(100,200)
    > >img3.src = 'someURL'
    > >
    > >I am trying to construct the placeholder through a loop as folows
    > >
    > >for(var i=1;i<pics;i++){
    > > ( img + i ) = new Image(100,200)
    > > (img + i).src = 'someURL'
    > >}
    > >
    > >what I don't know is how to join img and i

    >
    > Whenever you find yourself using eval(), you've probably
    > overlooked a simpler way to do something.
    >
    > var img=new Array();
    > for(var i=0;i<pics;i++){
    > img = new Image(100,200);
    > img.src = 'someURL';
    > }
    >
     
    Stuart, Nov 5, 2003
    #16
  17. Stuart

    Stuart Guest

    Douglas Crockford <> wrote in message
    news:2a40e$3fa96685$436563ac$...
    > > > Who told you to use eval()?
    > > >

    > > I am trying anything....including asking you!
    > > Are you getting the jist of what I am trying to achieve?

    >
    > It is almost always extremely wrong to use eval(). Who is teaching you to
    > program so badly?
    >


    And your input has been ?????
     
    Stuart, Nov 5, 2003
    #17
  18. Stuart

    Stuart Guest

    Thomas 'PointedEars' Lahn <> wrote in message
    news:bobps6$1c0h2d$-berlin.de...
    > Stuart wrote:
    >
    > > Thomas 'PointedEars' Lahn <> wrote [...]
    > > > However, [eval(...) is] still bad style.

    > >
    > > [...] May be it is "bad style" but I am new to javaScript, so may be you
    > > could point me in the way of "Good Style" and the correct way of
    > > scripting.

    >
    > I already did, what part of it did you not understand?


    I see nothing worthwhile that you have sugested!

    >
    > Maybe if you would shorten the quotes to what is required for following

    the
    > discussion, which saves disk space and bandwidth, and eases reading, you
    > were forced to read postings more thoroughly:



    If you are so concerned about bandwidth, why have you posted a somewhat
    pointless reply TWICE ??
     
    Stuart, Nov 5, 2003
    #18
  19. (HikksNotAtHome) writes:

    > eval, in and of itself, is not evil. Its mis-use and the mis-understanding of
    > its appropriate use is what makes it evil. I know of at least 2 places where
    > its use is actually *good* .


    Let us hear :)

    /L 'I use it too, but only in one place'
    --
    Lasse Reichstein Nielsen -
    JS Test Bench: <URL:http://www.infimum.dk/HTML/javascript/jstest6.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 6, 2003
    #19
  20. "Stuart" <> wrote in message
    news:bobsev$i4a$...
    > Many thanks Lee.....works perfectly

    <snip>
    >>>>>for(var i=0;i<pics;i++){
    >>>>> eval('img'+i) = new Image(wth,hgt)
    >>>>> eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif'

    <snip>

    What you are attempting is property access based on the result of
    evaluating an expression. That is a common misuse of eval and never
    necessary as JavaScript has this capability built in to its square
    bracket property accessor syntax. See:-

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

    - and the linked article for more information.

    Although, in this particular context where the only variable part of the
    expression is an integer that acts as an index, Lee's Array based
    approach is better suited to the problem.

    Richard.
     
    Richard Cornford, Nov 6, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,411
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    547
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,450
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    134
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    159
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page