Forum Moderators: open

Message Too Old, No Replies

array is not defined

... eventhough it is.

         

Hevymetal

8:28 am on Sep 4, 2004 (gmt 0)

10+ Year Member



Hi

I have created some functions that use arrays, but their size depends on situations that accour later. So at first i declare a empty array and later in my code i make new array of correct size. But still it says my array is not defined. Here is the script:


<SCRIPT LANGUAGE= JavaScript>

<!--

var pics=new array(); //first error
var imgObjs=new array();

function preload(pics){
var imgObjs=new array(pics.length); //second error
for (i=0; i<pics.length; i++){
imgObjs[i]=new Image();
imgObjs[i].src=pics[i];
}
}

//-->

</SCRIPT>
...
some php and html
...
<script language="JavaScript"><!--
var size=37;
var pics = new Array(size);
//assign picture src to array elements

preload(pics);
//-->
</script>

birdbrain

11:42 am on Sep 4, 2004 (gmt 0)



Hi there Hevymetal,

new array() should be....

new Array()

birdbrain

Bernard Marx

1:18 pm on Sep 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a tip:


var size=37;
var pics = new Array(size);
//assign picture src to array elements

Assigning a 'size' to an array in Javascript is not necessary. Javascript arrays are 'sparse'. They are very much unlike Java arrays. Assigning a length at time of creation only sets the length property, it doesn't create null or undefined members.

You might normally add a new member to an array like this:

myArray[myArray.length] = aMember

This will fail if you have preset the length.
What I'm saying is "don't bother setting the length".

Rambo Tribble

2:32 pm on Sep 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps it would be more fair to compare the dynamic personality of the JavaScript array to that of the Java ArrayList. Admittedly, the JavaScript array has a little friendlier personality, but I think the ArrayList is a bit more photogenic.

Hevymetal

6:19 am on Sep 5, 2004 (gmt 0)

10+ Year Member



Now it works ... thank you everybody.

Bernard Marx

9:27 am on Sep 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like birdbrain's double-bordered box.

birdbrain

9:59 am on Sep 5, 2004 (gmt 0)




Thank you Bernard for
your kind appreciation
of my presentational
skills ;) ;) ;) ;) ;)