Problem with arrays....

C

cjl

Hey all:

I've been banging my head against this one all day:

var cr_series = new Array(4,2,0);

function preloadCR()
{
for (loop = 0; loop <= (cr_series.length - 2); loop++)
{
for (cr_counter = 0; cr_counter <= (cr_series[loop]-1);
cr_counter++)
{
cr_images[loop] = new Array();
cr_images[loop][cr_counter] = new Image();
cr_images[loop][cr_counter].src = "images/cr" + (loop+1) + "_" +
(cr_counter+1) + ".jpg";
}
}
}

In the folder 'images' I have the following files:
cr1_1.jpg
cr1_2.jpg
cr1_3.jpg
cr1_4.jpg
cr2_1.jpg
cr2_2.jpg

When I try to access cr_images[0][0] I get an error "has no properties"

help?
 
L

Lee

cjl said:
Hey all:

I've been banging my head against this one all day:

var cr_series = new Array(4,2,0);

function preloadCR()
{
for (loop = 0; loop <= (cr_series.length - 2); loop++)
{
for (cr_counter = 0; cr_counter <= (cr_series[loop]-1);
cr_counter++)
{
cr_images[loop] = new Array();
cr_images[loop][cr_counter] = new Image();
cr_images[loop][cr_counter].src = "images/cr" + (loop+1) + "_" +
(cr_counter+1) + ".jpg";
}
}
}

In the folder 'images' I have the following files:
cr1_1.jpg
cr1_2.jpg
cr1_3.jpg
cr1_4.jpg
cr2_1.jpg
cr2_2.jpg

When I try to access cr_images[0][0] I get an error "has no properties"

help?

Move cr_images[loop] = new Array() outside of the inner loop.
You're recreating the array each time you add a value to it.
 
C

cjl

OK:

Let me simplify this.

The following works:

var i = 0;
cr_images[0] = new Array();
cr_images[0] = new Image();
cr_images[0].src = "images/cr1_1.jpg";

The following does not:

for (var i = 0; i<4; i++)
{
cr_images[0] = new Array();
cr_images[0] = new Image();
cr_images[0].src = "images/cr1_1.jpg";
}

With the first example, I can access cr_images[0][0].src
With the second example I get an error that cr_images[0][0] has no
properties.

What is up?

CJL
 
V

VK

A BIG bug in JavaScript mechanics. It fails to deal properly with the
loop variable used as object references (doesn't eval them properly at
the runtime).

An evangelism problem. You need look for a way around.
 
J

Jim Ley

The following does not:

for (var i = 0; i<4; i++)
{
cr_images[0] = new Array();
cr_images[0] = new Image();
cr_images[0].src = "images/cr1_1.jpg";
}

With the first example, I can access cr_images[0][0].src
With the second example I get an error that cr_images[0][0] has no
properties.

What is up?


Think about what happens, let's expand the behaviour as if the loop
wasn't there:


cr_images[0] = new Array();
cr_images[0][0] = new Image();
cr_images[0][0].src = "images/cr1_1.jpg";

cr_images[0] = new Array();
cr_images[0][1] = new Image();
cr_images[0][1].src = "images/cr1_1.jpg";

(and 2,3 but we'll imagine the loop is just 2)

so after the first iteration, cr_images[0] is reset to a new array,
destroying the previous array that contained the image in index 0.

move the cr_images[0] outside the loop, and it works.

Jim.
 
L

Lee

VK said:
A BIG bug in JavaScript mechanics. It fails to deal properly with the
loop variable used as object references (doesn't eval them properly at
the runtime).

What are you talking about?
 
M

Michael Winter

A BIG bug in JavaScript mechanics.

You've waved the word 'bug' around several times before, and every time
you've just proved you don't understand how something works. You might
want to stop.
It fails to deal properly with the loop variable used as object
references (doesn't eval them properly at the runtime).

Are you referring to something like:

var myArray = [],
myObject1 = {},
myObject2 = {};

myArray[myObject1] = 'some value';

myArray[myObject2] // 'some value'

Again, this is clearly defined behaviour, and in no way a bug. Why it
happens has been explained several times.

If you're referring to something else, post something that demonstrates
this 'bug' (and post properly).

Mike
 
V

VK

this is clearly defined behaviour

By your books any stuff becomes His word as soon as it's spelled by W3.
By my books a stuff *may* be a nonsense as long as it violates the
end-used experience/expectations (see my definition of the "end-user"
in the older topic).

I'm really trying to love the Big Brother (W3) as much as I can. And
I'm really sorry if I fail sometimes. I just have badly good memory I
guess. In the particular I still remember that at the morning of the
day the Buttle Of The 4th Versions began (which defined the winner of
the Great Browser Wars) - W3 received just two links from Netscape and
Microsoft: with a proposal to either download it and write standards
from it; or to go to hell whatsoever. W3 even complained to ISA about
it... An old story though...

Also during my 15 years experience I've been learned that you never
deal with standards. You always deal with particular engines build
more-or-less upon these standards. Sometimes it does more than the
standards promise, sometimes it does lesser than it should. The only
right answer can be obtained from a test.

P.S. The above discussed array behavior is fully ECMA-compliant.
 
C

Christopher J. Hahn

VK said:
By your books any stuff becomes His word as soon as it's spelled by W3.
By my books a stuff *may* be a nonsense as long as it violates the
end-used experience/expectations (see my definition of the "end-user"
in the older topic).

I'm really trying to love the Big Brother (W3) as much as I can. And
I'm really sorry if I fail sometimes. I just have badly good memory I
guess. In the particular I still remember that at the morning of the
day the Buttle Of The 4th Versions began (which defined the winner of
the Great Browser Wars) - W3 received just two links from Netscape and
Microsoft: with a proposal to either download it and write standards
from it; or to go to hell whatsoever. W3 even complained to ISA about
it... An old story though...

Also during my 15 years experience I've been learned that you never
deal with standards. You always deal with particular engines build
more-or-less upon these standards. Sometimes it does more than the
standards promise, sometimes it does lesser than it should. The only
right answer can be obtained from a test.

P.S. The above discussed array behavior is fully ECMA-compliant.

Of course it's ECMA-compliant, and it does exactly as it's being told.
Nothing about the behavior described is at all what I or any other
programmer possessed of all his faculties should expect.

It was a logic error, not a language or implementation bug. Inspection
of the logic in the code makes that pretty clear, and it's either
[excess] laziness or [excess] hubris that obfuscates it.

When you redefine a variable, you should expect its old value to be
removed. Few languages (see also: PERL::Tie) behave otherwise, unless
you deliberately overload the assignment operator.

Look at the code again and actually step through what each line does.
You know. Debug it. Then point out exactly where the language is
behaving as it ought not, or not behaving as it ought, and why.
 
M

Michael Winter

By your books any stuff becomes His word as soon as it's spelled by W3.

Despite what you might think, this has nothing to do with the W3C, or
ECMA either. This is simply a matter of how the language works, and in
your very own words, "you deal with particular engines" or rather,
implementations (and they all agree on this matter).
By my books a stuff *may* be a nonsense as long as it violates the
end-used experience/expectations [...]

Though many languages operate in similar ways, each is different and has
its own unique features and quirks. Trying to apply knowledge of one
language /directly/ to another might afford you some progress initially,
but it's no way to go about using it properly.

It still seems that you're trying to treat objects in ECMAScript as
collection objects that one might find in C++ or Java. While these other
languages do treat objects as unique keys (subject to some constraints)
in collections, native ECMAScript objects are /not/ collections. They
are objects with dynamic properties, and therefore have different rules.
An object cannot be a property name, as property names are strings,
however the toString value of that object can, and is, used to form such
a name.

I didn't understand this particular feature at one point in the
not-too-distant past, and I was thoroughly embarrassed when that was
pointed out, rendering what I had posted at the time completely useless.
No-one can possibly expect someone to understand /everything/ about a
language before they start using it, but when you are proved wrong,
learn from it, accept it, and move on. Trying to resist that process
only prolongs any agony.

[snip]

Mike
 
R

Richard Cornford

VK said:
By your books any stuff becomes His word as soon as it's
spelled by W3. By my books a stuff *may* be a nonsense as
long as it violates the end-used experience/expectations
(see my definition of the "end-user" in the older topic).

So you are saying that a language implementation is wrong if it doesn't
do exactly what each and every individual programmer expects it to do
(regardless of their knowledge of, and experience in, that language).
That is such a bizarre and unrealistic criteria that it would likely be
taken as a symptom of mental illness.

Looking at the OP's code, you are saying that because the OP does not
expect the assignment of a newly created Array object to an indexed
member of another Array to replace any references already assigned to
that Array member, then the language should not make the assignment.

But the next programmer out of the hat is likely to expect assigning a
reference to a newly created Array object to a member of another Array
to replace any pre-existing references. The language cannot do both
without being able to literally read the mind of the programmer in
question (not a realistic expectation of a programming language).
Somewhere there has to be a statement of what the language should do, in
all cases, and independent of the beliefs and attitudes of anyone trying
to use that language. And that statement is to be found in ECMA 262.

Or are you saying that the language is wrong when its behaviour does not
conform with your personal (and ill-informed) expectations? That would
be pushing ego into insanity.
I'm really trying to love the Big Brother (W3) ...
... . W3 even complained to ISA about it... An old story though...

Javascript is standardised by the ECMA, the W3C has nothing to do with
defining the behaviour of programming languages.
Also during my 15 years experience I've been learned
that you never deal with standards.

Which might explain why 15 years of experience leaves you creating such
catastrophically bad code. I learnt enough in my first couple of years
programming that I would be utterly ashamed to even be contemplating
writing some of the code you have been posting here in all seriousness
over the last couple of months.

You should recall what happen with the event handlers. You spent months
failing to achieve what you wanted, whinging about buggy browsers,
dismissing and disregarding the explanations of how it could be achieved
provided by others, and trying to convince innocent bystanders that the
whole thing was complex to the pint of being impossible. But when it
came down to it the solution turned out to be one_line_of_code that
could have been provided by any of the regular contributors to this
group in exchange for a well-phrased, precise, question on the subject.

Judging only by its results, your entire approach is wrong. It is not
productive for you, it wastes the time of others and it certainly should
not be recommended to anyone.
You always deal with particular engines build
more-or-less upon these standards.

When scripting web browsers for the Internet you are not working with
particular engines, you are dealing with an unknown range of engines.
Sometimes it does more than the standards promise,
sometimes it does lesser than it should. The only
right answer can be obtained from a test.

A test is a futile activity without objective criteria to judge the
result.
P.S. The above discussed array behavior is fully ECMA-compliant.

Which "above discussed array behaviour"? The behaviour you described as
"A BIG bug in JavaScript mechanics"? Nothing on the subject has been
quoted by you.

Richard.
 
L

Lee

VK said:
Also during my 15 years experience I've been learned that you never
deal with standards.

You should have learned what "bug" means in those 15 years.
It does not mean behavior that you, personally, do not expect.

If you want to use Objects as unique indices into an Array,
all you have to do is to define a toString() method for that
custom object type, and ensure that it returns a unique value
for each instance.

That has nothing to do with the problem in this thread, of
course.
 
V

VK

Let me explain why I consider it a nonsense, no matter how many
paragraphs are written in W3 / ECMA / Anyone docs.

Besides the OP testcase you may also look at this old postings with a
similar problem:
<http://groups-beta.google.com/group...ript+author:VK&rnum=30&hl=en#0ea74b9d30854578>

*IMHO* we have a violation of our synchronization exprerience. For
example, if I do:
var c = a + b;
I expect that I'll receive *the current values* of (a) and (b), not
future ones.

Same way if I use an array element like arrayObject in any context,
I have the right to expect that this context will *always* have the
value of the element pointed by (i) at the exact moment I used it.
So if at 9:00:01:0001 PM I used say
var obj = arrayObject;
and (i) was equal to 1 at that very moment, it should be always 1 in
this given context. I don't want any other values of (i), and I don't
care *within this context* if (i) became equal say null at 9:00:01:0300
PM
 
L

Lee

VK said:
Let me explain why I consider it a nonsense, no matter how many
paragraphs are written in W3 / ECMA / Anyone docs.

Besides the OP testcase you may also look at this old postings with a
similar problem:
<http://groups-beta.google.com/group...ript+author:VK&rnum=30&hl=en#0ea74b9d30854578>

*IMHO* we have a violation of our synchronization exprerience. For
example, if I do:
var c = a + b;
I expect that I'll receive *the current values* of (a) and (b), not
future ones.

Same way if I use an array element like arrayObject in any context,
I have the right to expect that this context will *always* have the
value of the element pointed by (i) at the exact moment I used it.


Sure. But if arrayObject is a reference to some Object, you have
the right to expect it to refer to that Object, which or may not have
had some attributes change in the meantime.
 
L

Lasse Reichstein Nielsen

VK said:
Let me explain why I consider it a nonsense, no matter how many
paragraphs are written in W3 / ECMA / Anyone docs.

Consider what nonsense? Please quote some context!
*IMHO* we have a violation of our synchronization exprerience. For
example, if I do:
var c = a + b;
I expect that I'll receive *the current values* of (a) and (b), not
future ones.

That depends on what you mean by "do". If you evaluate the expression,
you get the current values, because evaluating a variable gives its
value.

If you just write the code inside a function, then it's not evaluated.
The function then only stores the variables, not their values. When
the body of the function is evaluated, those variables evaluate to
their current value at that time.
---
var x = 42;
function f() { alert(x); }
x = 37;
f()
x = "Happy Kayaking";
f();
---
This is the behavior that people expect in many cases, e.g., if
you write code like:
---
var counter = 0;
setInterval( function() {
someObj.style.left= counter + "px";
counter++;
}, 200);
---
then you expect each new call of the function to use the updated
value of "counter". Why is it that you expect it differently
in other cases? What is the difference?


It's the same in all languages that have both closures and side
effects, e.g., Scheme:

(define x 42)
(define f (lambda () x))
(set! x 37)
(f) ;; yields 37.

Same way if I use an array element like arrayObject in any context,
I have the right to expect that this context will *always* have the
value of the element pointed by (i) at the exact moment I used it.


Again, you should clarify what you mean by "use". I guess you mean
"write", not "evaluate". In that case, you are still going against
all existing languages.


When discussing details of semantics, it is necessary to use precise
wording.
/L
 
V

VK

If you just write the code inside a function,
then it's not evaluated. The function then only
stores the variables, not their values. When
the body of the function is evaluated, those variables
evaluate to their current value at that time.

Yes, the closures issue crossed my mind too. But I guess that the "real
user intentions" are expressed clearly enough in the type of variable.
If variable "i" is a global variable (or a literal from the function
arguments list) then it should stay "alive" and be evaluated upon each
call.

If I put arrayObject in come context and "i" is a local variable, I
definitely want to have arrayObject here (where "i" holds the value
it had at the moment of the creation of this context). I definitely
don't want to deal with arrayObject[undefined] not now, not later.

Actually some kind of "transient" variable property would help here? Or
some lockValue() method? Just to keep the code always easy to read.
 

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

Forum statistics

Threads
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top