This could be a problem with JS Fiddle, but I am using console.log()
to print the values of a collection of objects.
First I initialize the object collection with some data (some objects) and console log it.
Then I update this collection with some new data and console log it.
What is happening is that the first and second console logs are both identical, even though the object data was changed. I would like to know if this is a bug, or if I am doing something wrong.
http://jsfiddle.net/n302nsbh/18/
function FooManager() {
this.objects = {};
this.update = function(data) {
var self = this;
$.each(data, function(i, e) {
var foo = self.objects[i];
if (typeof foo === "undefined") {
foo = new Foo(e);
self.objects[i] = foo;
} else if (foo instanceof Foo) {
foo.update(e);
}
});
}
return this;
}
function Foo(data) {
this.name = data.name;
this.age = data.age;
return this;
}
Foo.prototype.update = function(data) {
this.name = data.name;
this.age = data.age;
}
//------ Update 1 --------//
var appData = {
"0": {
name: "a",
age: 2
},
"1": {
name: "b",
age: 3
}
}
var fooManager = new FooManager();
fooManager.update(appData);
console.log(fooManager.objects);
//------ Update 2 --------//
var newAppData = {
"0": {
name: "a",
age: 443
}
}
fooManager.update(newAppData);
console.log(fooManager.objects);
Both update 1 and update 2 logs are identical!