473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

object and functions

dreamfalcon
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
7 1539
hsriat
1,654 Expert 1GB
Add a closing brace and a semicolon }; after line 17
May 12 '08 #2
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
1,654 Expert 1GB
Is the error still there?
May 13 '08 #4
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
"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
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
1,654 Expert 1GB
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

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

Similar topics

6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Gawain Lavers | last post by:
I have a script which uses a lot of object augmentation (in order to extend the functionality of DOM elements), and I'm clearly stressing out my browser (I rapidly get to a point where Firefox is...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
4
by: Daniel | last post by:
Hi, I was reading Douglas Crockford's article on prototypal inheritance at http://javascript.crockford.com/prototypal.html. I think it also relates to a post back in Dec 2005. The mechanism...
1
by: Bryan Parkoff | last post by:
An object can be defined using a class. The class contains variables and functions. It has a pointer to bind variables and functions. If I want to create more than one object. The class can...
0
markrawlingson
by: markrawlingson | last post by:
Hello, So I've got a list of functions written in ASP Classic which I include into every page within my application(s). This file is getting quite big and in most scenarios needs to be called 4-5...
10
by: sumsin | last post by:
The C++ Object Model book says that 'Nonstatic data members are allocated directly within each class object. Static data members are stored outside the individual class object. Static and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.