I have the following code in my website:
function GroupObject(GroupID, GroupColor, GroupName, CalendarID, UserEnable, IrcChannel) {
this.uid = GroupID;
this.color = GroupColor;
this.groupname = GroupName;
this.calendarid = CalendarID;
this.userenable = UserEnable;
this.ircchannel = IrcChannel;
}
function GetGroupObjects(callback) {
var GlobalDB = [];
$.getJSON("Some Data From Google Docs",
function (data) {
$.each(data.feed.entry, function (i, entry) {
GlobalDB.push(new GroupObject(entry.gsx$uid.$t,
"000000",
SanitizeInputText(entry.gsx$group.$t),
SanitizeInputCalID(entry.gsx$calendarid.$t),
true,
SanitizeInputText(entry.gsx$ircchannel.$t)))
});
console.log(GlobalDB[0]);
console.log(GlobalDB[0].color);
callback(GlobalDB);
});
};
All the parameters of the newly created GlobalDB are correct with the only exception of the parameter “color”. console.log(GlobalDB[0]) returns:
GroupObject
calendarid: "CalOfTNG"
color: "AB8B00"
groupname: "Austin TNG"
ircchannel: "AustinTNG"
uid: "TNG"
userenable: true
__proto__: GroupObject
It brings the same value for color “AB8B00” in both Chrome and Firefox. Any idea why? From the code above it should be 0. console.log(GlobalDB[0].color) does returns 000000. But when
I use GlobalDB when returning from the callback I get again AB8B00.
user enable, on the other hand works just fine. I just cannot find what is causignt he problem with the parameter .color as it fails in both Chrome and Firefox.
Thanks in advance.
You seem to be subject to a
console.logproblem I have experienced often: you don’t see the object exactly as it is when logged but as it is later, because the browser doesn’t deep clone it immediately when you log but just stores its reference.This effect doesn’t affect primitive, like strings, that’s why the color appears initially fine when you log
GlobalDB[0].color.The color is “000000” when you log it. It changes after, probably when you call
callback(GlobalDB).See also this related question (and answer).