eval

S

Stuart

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
 
T

Thomas 'PointedEars' Lahn

Stuart said:
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
 
D

Douglas Crockford

why won't the following work
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()?
 
L

Lee

Stuart said:
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';
}
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Stuart said:
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
 
S

Stuart

Douglas Crockford said:
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
 
D

Douglas Crockford

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?
 
J

Janwillem Borleffs

Stuart said:
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
 
S

Stuart

Thomas 'PointedEars' Lahn said:
Thomas said:
Stuart said:
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.
 
T

Thomas 'PointedEars' Lahn

Stuart wrote:

[Repaired quoting, please read
http://home.in.tum.de/~jain/software/oe-quotefix/]
Thomas 'PointedEars' Lahn said:
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
 
T

Thomas 'PointedEars' Lahn

Stuart said:
Thomas 'PointedEars' Lahn said:
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
 
D

Douglas Crockford

Who told you to use eval()?
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.
 
T

Thomas 'PointedEars' Lahn

Stuart said:
Thomas 'PointedEars' Lahn said:
Stuart said:
[...] 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 said:
^^
Your newsreader is b0rken. Please read
http://home.in.tum.de/~jain/software/oe-quotefix/
and fix it.
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
 
S

Stuart

Many thanks Lee.....works perfectly

thanks again
Stuart


Lee said:
Stuart said:
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';
}
 
S

Stuart

Thomas 'PointedEars' Lahn said:
Stuart said:
Thomas 'PointedEars' Lahn said:
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 ??
 
L

Lasse Reichstein Nielsen

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'
 
R

Richard Cornford

Stuart said:
Many thanks Lee.....works perfectly
<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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top