By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,256 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,256 IT Pros & Developers. It's quick & easy.

object and functions

dreamfalcon
P: 20
Hi
I had some variables and functions to work with google maps, that are working great, but i needed to have more than one map in the same page, and was using different names to each variable, like xml1, xml2. But decided to use a object to group all variables and functions.

Expand|Select|Wrap|Line Numbers
  1. function Map(){
  2.     this.xml;
  3.  
  4.     this.loadMap = function (tabId){
  5.         this.myElement = $(tabId);
  6.         if (GBrowserIsCompatible()) {
  7.             this.map = new GMap2(this.myElement.getElementsByClassName("googlemap")[0]);
  8.         ...
  9.         this.getCells(tabId);
  10.         }
  11.     }
  12.  
  13.     this.getCells = function(){
  14.         GDownloadUrl("request.php?script=genxml.php", function (data){
  15.             this.xml = GXml.parse(data);
  16.             this.redraw();
  17.         });
  18.  
  19.     this.redraw = function (){
  20.         alert("ok");
  21.         //code to draw cells
  22.     }
  23. }
  24.  
and
Expand|Select|Wrap|Line Numbers
  1. map1= new Map();
  2. map1.loadMap(tabId);
  3.  
it gives this error
"this.redraw is not a function"

and map1.xml is empty.

I don´t have many experience with objects in Javascript
Can anyone help me?
thx
May 12 '08 #1
Share this Question
Share on Google+
7 Replies


hsriat
Expert 100+
P: 1,654
Add a closing brace and a semicolon }; after line 17
May 12 '08 #2

dreamfalcon
P: 20
Add a closing brace and a semicolon }; after line 17
sorry, copy paste error :D

Expand|Select|Wrap|Line Numbers
  1. function Map(){
  2.     this.xml;
  3.  
  4.     this.loadMap = function (tabId){
  5.             this.myElement = $(tabId);
  6.         if (GBrowserIsCompatible()) {
  7.             this.map = new GMap2(this.myElement.getElementsByClassName("googlemap")[0]);
  8.         ...
  9.         this.getCells(tabId);
  10.         }
  11.     }
  12.  
  13.     this.getCells = function(){
  14.         GDownloadUrl("request.php?script=genxml.php", function (data){
  15.             this.xml = GXml.parse(data);
  16.             this.redraw();
  17.         });
  18.       }
  19.     this.redraw = function (){
  20.         alert("ok");
  21.         //code to draw cells
  22.     }
  23. }
  24.  
and
Expand|Select|Wrap|Line Numbers
  1. map1= new Map();
  2. map1.loadMap(tabId);
  3.  
May 13 '08 #3

hsriat
Expert 100+
P: 1,654
Is the error still there?
May 13 '08 #4

dreamfalcon
P: 20
Is the error still there?
"this.redraw is not a function"
Yes, same error :(

Maybe because is a function inside a function, it doesn't recognize as the same object?
May 13 '08 #5

dreamfalcon
P: 20
"In JavaScript this always refers to the “owner” of the function..."

In my case the “owner” becomes GDownloadUrl.
After a google search i think that this is called "Higher order functions", but no ideas how to solve this.
May 13 '08 #6

dreamfalcon
P: 20
SOLVED
-> var me=this;

Expand|Select|Wrap|Line Numbers
  1. function Map(){
  2.     this.xml;
  3.     var me=this;
  4. ...
  5.  
  6.     this.getCells = function(){
  7.         GDownloadUrl("request.php?script=genxml.php", function (data){
  8.             me.xml = GXml.parse(data);
  9.             me.redraw();
  10.         });
  11.       }
  12.  
  13. }
  14.  
May 13 '08 #7

hsriat
Expert 100+
P: 1,654
That's good you solved it!!

Even I hate the fact that I don't understand OOP. Will start from a..b..c.. in some free time.
May 13 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.