Connecting Tech Pros Worldwide Help | Site Map

this/prototype problem?

allen.leis@gmail.com
Guest
 
Posts: n/a
#1: Aug 16 '06
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>

INeedADip
Guest
 
Posts: n/a
#2: Aug 16 '06

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

allen.leis@gmail.com
Guest
 
Posts: n/a
#3: Aug 17 '06

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


Similar JavaScript / Ajax / DHTML bytes