This is topic JavaScript problems in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=053209

Posted by King of Men (Member # 6684) on :
 
I'm just learning this language, and a little bit frustrated at the moment. I am trying to create an object which stores two arrays, x and y coordinates. Like so:

code:
function jsProvince (px, py, n) {
this.polyx = px.slice();
this.polyy = py.slice();
this.name = n;
}

I initialise this object in my main html:

code:
  var polyx = new Array(210,220,220,210);
var polyy = new Array(10,10,20,20);
aProvince = new jsProvince(polyx, polyy, "city");

Now I try to use it in a function called with 'onclick':

code:
 if (isPointInPoly(myProvince.polyx, myProvince.polyy, xpos, ypos)) {
// Do some thing
}

which is where the problem arises: The polyx array has length zero. Presumably I did not initialise it correctly, but I do not understand what I am doing wrong. Any insights?
 
Posted by Lisa (Member # 8384) on :
 
Is the initialization in the same script block? And is it there before the function gets called?

Do you have the script at the top of the page or the bottom?
 
Posted by TomDavidson (Member # 124) on :
 
Um.
I might be overlooking something here, but it looks like you're initializing "aProvince" as an instance of jsProvince, but trying to reference the instance "myProvince." Do you initialize "myProvince" anywhere?
 
Posted by King of Men (Member # 6684) on :
 
Wups. I'm actually initialising a whole array of the things, I tried to simplify the code to show the problem's essential features without getting bogged down in all the details.

quote:
Is the initialization in the same script block? And is it there before the function gets called?

Do you have the script at the top of the page or the bottom?

The sequence is:

Define functions by including script files
Define image with onclick that calls function
Initialise variables.

So it is separate script blocks, but I had the impression that JavaScript variables are global by default?
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2