FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » JavaScript problems

   
Author Topic: JavaScript problems
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
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?
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Lisa
Member
Member # 8384

 - posted      Profile for Lisa   Email Lisa         Edit/Delete Post 
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?

Posts: 12266 | Registered: Jul 2005  |  IP: Logged | Report this post to a Moderator
TomDavidson
Member
Member # 124

 - posted      Profile for TomDavidson   Email TomDavidson         Edit/Delete Post 
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?

Posts: 37449 | Registered: May 1999  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
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?

Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

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