I'm using the Prototype.js (from www.prototypejs.org) library to create classes which are extended by subclasses. I'm having trouble using arrays within instances of these classes though. I've made an example to illustrate this:
var SuperClass = Class.create({
initialize: function(id) {
this.id = id;
}
});
var SubClass = Class.create(SuperClass, {
items: [],
add: function(arg) {
this.items[this.items.length] = arg;
},
initialize: function($super, id) {
$super(id);
}
});
var a = new SubClass("a");
var b = new SubClass("b");
a.add("blah");
alert(a.id + ": " + a.items.length);
alert(b.id + ": " + b.items.length);
The problem here is that both the first and the second alert will indicate that their respective objects have 1 item in their items array, even though I only added an item to object a. Primitive data types work correctly (as is shown by the id property correctly showing up) but arrays just won't work. It also doesn't matter if I move the items array and the add method to the superclass, I already tried.
Is there some way of getting this to work? Is this a bug in Prototype or is this something that is in JavaScript's nature? :-)