Hey all - I was making a few objects using the prototype.js library and
ran into a problem. Specifically I'm trying to create a Squadron class
which contains an array of Platform class. All Squadron classes are
themselves kept in an array in the Squadrons class. However, after I
assign platforms to a specific squadron, all squadrons contain these
platforms! I'm assuming I'm not properly using the prototype or "this"
javascript features. Any help would be appreciated.
- Allen
<script src="scriptaculous/prototype.js"
type="text/javascript"></script>
<script>
var Squadron = Class.create();
Squadron.prototype = {
title : "",
id : "",
platforms : new Array(),
initialize : function(squadron_id, squadron_title) {
this.title = squadron_title;
this.id = squadron_id;
},
get : function (platform_id){
ret = {};
this.platforms.each (function(s) {
ret = (p.id == platform_id) ? p : ret;
});
return ret;
},
add : function(platform_id, platform_title) {
var temp = new Platform(platform_id,platform_title);
this.platforms[this.platforms.length] = temp;
},
dump : function() {
alert("Squadron Title : "+ this.title + "\nId : " + this.id);
this.platforms.each(function(p) {
p.dump();
});
}
};
var Platform = Class.create();
Platform.prototype = {
title : "",
id : "",
initialize : function(platform_id, platform_title) {
this.title = platform_title;
this.id = platform_id;
},
dump : function() {
alert("Platform Title : "+ this.title + "\nId : " + this.id);
}
};
var Squadrons = Class.create();
Squadrons.prototype = {
collection : new Array(),
initialize : function() {
},
add : function(squadron_id, squadron_title) {
var temp = new Squadron(squadron_id, squadron_title);
this.collection[this.collection.length] = temp;
},
get : function (squadron_id){
ret = {};
this.collection.each (function(s) {
ret = (s.id == squadron_id) ? s : ret;
});
return ret;
},
dump : function() {
this.collection.each(function(s) {
s.dump();
});
}
};
var mySquadrons = new Squadrons();
mySquadrons.add(1,"HX-21");
mySquadrons.get(1).add(1,'CH-53e');
mySquadrons.get(1).add(2,'TH-57c');
mySquadrons.add(2,"VX-20");
mySquadrons.dump();
</script> 2 1232
Now I thought that is what SomeClass.prototype was all about.
The prototype applies to all SomeClass objects.
Wouldn't you want to do something like:
Squadrons = function () {
var privateVariable = '';
return {
title : "",
get : function (plaform_id) {...},
add : function (platform_id) {...},
bla : function ()
}
}
var mySquads = new Squadrons();
bla bla bla...
First, thanks for the reply. Using the object literal syntax in my
example I cannot "var" any of the object properties without throwing an
error. I can try rewriting each object as a function but I know that
the current code "should" work. The prototype does created a shared
inheritence but shouldnt be sharing variables especially as I am
instantiating new instances of the classes.... I agree that it appears
to be doing so...
I would prefer to solve the problem in case this comes up again in the
future so any other thoughts are appreciated.
allen
INeedADip wrote:
Now I thought that is what SomeClass.prototype was all about.
The prototype applies to all SomeClass objects.
Wouldn't you want to do something like:
Squadrons = function () {
var privateVariable = '';
return {
title : "",
get : function (plaform_id) {...},
add : function (platform_id) {...},
bla : function ()
}
}
var mySquads = new Squadrons();
bla bla bla...
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gerald S |
last post by:
hi,
got a performance problem; situation is as described below (see "Version
Inline"), i've got a javascript class item with many methods defined
inside and with every call to "new item()" the...
|
by: mambenanje |
last post by:
I am using the prototype ajax library but I realised that each time I
am calling the server for xml the new records get appended to the old
so when I display the xml data it shows the old ones and...
|
by: Chaprasi |
last post by:
Hi I need help with prototype while doing ajax calls. So this is my JS
which does a ajax calls
var ajax1 = new Ajax.Request (
url,
{
method: 'get',
parameters: params,
onComplete:...
|
by: dougwig |
last post by:
I'm trying to handle the scenario where a user's session times out and
and their ajax request triggers a redirection by the webserver (302
error?).
I'm using Prototype 1.4 and the my works great...
|
by: James Black |
last post by:
I am working on my own pop up calendar, mainly because the one I am
currently using crashes the Safari browser at times.
So, I want to verify that what I am doing will work, in that I want to
be...
|
by: Raman |
last post by:
Hi All,
Here is a small Code,
int main(void)
{
char *p=(char *) malloc(100);
strcpy(p,"Test1234567890");
p=p+10;
free(p);
|
by: libsfan01 |
last post by:
Hi all
Im trying to use prototype for an xmlhttprequest, but it doesn't seem
to be working cross-browser. Is there someway of getting it to be IE6
compatible (active x)?
here's my code so...
|
by: Pallav singh |
last post by:
Hi ,
when should i select Factory Method / Prototype Design Pattern
during my design phase ??
as both look similar to me
Thanks in Advance
Thanks
Pallav
|
by: ahilar12 |
last post by:
1. <head>
2. <script type="text/javascript">
3. </script>
4. </head>
5. <body>
6. <form>
7. <select name="team" id="mylist" >
8. <option></option>
9....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |