I'd like to know why if I pass an object of arguments to my constructor, the setting property values get overwritten by subsequent instantiations.
This doesn't happen when I pass arguments as separate vars to the constructor.
Example here: http://jsfiddle.net/KJ4CU/ and below
// First class
function Person(args) {
for(s in args) {
this.arguments[s] = args[s];
}
}
Person.prototype.arguments = {
"gender" : null,
"name" : null
};
Person.prototype.getGender = function() {
return this.arguments.gender;
};
// Second class
function Animal(gender, name) {
this.gender = gender;
this.name = name;
}
Animal.prototype.getGender = function() {
return this.gender;
};
var p1 = new Person({ gender : "gal", name : "Jane"});
var p2 = new Person({ gender : "boy", name : "John"});
var a1 = new Animal("female", "Tina");
var a2 = new Animal("male", "Toto");
document.getElementById("p1").innerHTML = p1.getGender(); // boy
document.getElementById("p2").innerHTML = p2.getGender(); // boy
document.getElementById("a1").innerHTML = a1.getGender(); // female
document.getElementById("a2").innerHTML = a2.getGender(); // male