Connecting Tech Pros Worldwide Help | Site Map

this/prototype problem?

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 16th, 2006, 09:05 PM
allen.leis@gmail.com
Guest
 
Posts: n/a
Default this/prototype problem?

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  
Old August 16th, 2006, 10:25 PM
INeedADip
Guest
 
Posts: n/a
Default Re: this/prototype problem?

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...

  #3  
Old August 17th, 2006, 12:25 PM
allen.leis@gmail.com
Guest
 
Posts: n/a
Default Re: this/prototype problem?

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:
Quote:
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...
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.