Every instance shares the same this.settings
object. Mutable data structures have to be initialized in the constructor, not on the prototype:
Test.prototype = {
initialize: function(options) {
this.settings = {
a: {},
b: {},
c: options.c;
};
}
};
Note that this appears to be the "legacy way" (according to the documentation) and the newer way is:
var Test = Class.create({
initialize: function(options) {
this.settings = {
a: {},
b: {},
c: options.c;
};
}
});
This issue is actually addressed in the PrototypeJS documentation:
Types of inheritance in programming languages
Generally we distinguish between class-based and prototypal inheritance, the latter being specific to JavaScript.
Prototypal inheritance, of course, is a very useful feature of the language, but is often verbose when you are actually creating your objects. This is why we emulate class-based inheritance (as in the Ruby language) through prototypal inheritance internally. This has certain implications.
[...]
We can try to do the same in Prototype:
var Logger = Class.create({
initialize: function() { },
log: [],
write: function(message) {
this.log.push(message);
}
});
var logger = new Logger;
logger.log; // -> []
logger.write('foo');
logger.write('bar');
logger.log; // -> ['foo', 'bar']
It works. But what if we make another instance of Logger?
var logger2 = new Logger;
logger2.log; // -> ['foo', 'bar']
// ... hey, the log should have been empty!
You can see that, although you may have expected an empty array in the new instance, it has the same array as the previous logger instance. In fact, all logger objects will share the same array object because it is copied by reference, not by value.
Instead, initialize your instances with default values:
var Logger = Class.create({
initialize: function() {
// this is the right way to do it:
this.log = [];
},
write: function(message) {
this.log.push(message);
}
});