Here is the code:
//Note: x actually isn't defined, I'm pulling it from an external source
var x = [{ name: 'Michael Lovesllamas Lankford',
created: 1338420951.11,
laptop: 'pc',
laptop_version: null,
userid: '4fc6aed7eb35c14ad6000057',
acl: 0,
fans: 1,
points: 5,
avatarid: 34 }]
global.UserBase = {
userid: -1,
name: "noidea",
isSuperUser: false,
isDJ: false,
laptop: "pc",
afkWarned: false,
afkTime: Date.now(),
droppedTime: null,
droppedRoom: null,
songCount: 0,
mWaitingSongLimit: 0,
totalSongCount: 0,
totalHeartCount: 0,
totalHeartsGiven: 0,
customGreeting: null,
bootAfterSong: false,
joinedTime: Date.now(),
whiteList: false,
allowedToReserveSpot: true
};
global.mUsers = {length:0};
global.Register = function(a) {
for(var i = 0; i < a.length; i++) {
var sUser = a[i];
mUsers[sUser.userid] = CreateUser(sUser);
mUsers.length++;
}
};
global.CreateUser = function(a) {
var b = Object.create(UserBase);
b.userid = a.userid;
b.name = a.name;
b.laptop = a.laptop;
if (a.acl > 0) b.isSuperUser = true;
return b;
};
Register(x);
Now, to the problem. Instead of mUsers[sUser.userid] becoming this:
'4fc6aed7eb35c14ad6000057': {
userid: "4fc6aed7eb35c14ad6000057",
name: "noidea",
isSuperUser: false,
isDJ: false,
laptop: "pc",
afkWarned: false,
afkTime: Date.now(),
droppedTime: null,
droppedRoom: null,
songCount: 0,
mWaitingSongLimit: 0,
totalSongCount: 0,
totalHeartCount: 0,
totalHeartsGiven: 0,
customGreeting: null,
bootAfterSong: false,
joinedTime: Date.now(),
whiteList: false,
allowedToReserveSpot: true
}
it becomes this:
'4fc6aed7eb35c14ad6000057': {
userid: '4fc6aed7eb35c14ad6000057',
name: 'Michael Lovesllamas Lankford',
laptop: 'pc'
}
Any ideas why the rest of the values in UserBase aren't being added to the object?
UserBase
is on global
object, and you need to call Register
on global
object.
global.CreateUser = function(a) {
var b = Object.create(this.UserBase);
b.userid = a.userid;
b.name = a.name;
b.laptop = a.laptop;
if (a.acl > 0) b.isSuperUser = true;
return b;
};
global.Register(x);
this is happening beacause you are only initializing the values for userid, name and laptop. see your code:
b.userid = a.userid;
b.name = a.name;
b.laptop = a.laptop;
to get the desired result, you need to call Global.Register(x) instead of Register(x)
Object.create
creates a new object and sets its prototype
to the object you pass in.
Your object is getting initialized, but you're only explicitly setting a few properties on the new object instance. The properties from the prototype object (UserBase
) are all accessible, but a direct console.log
of the object won't print them.
E.g. after running your code, doing:
for(var p in mUsers['4fc6aed7eb35c14ad6000057']) {
console.log(p, mUsers['4fc6aed7eb35c14ad6000057'][p]);
}
prints out:
userid 4fc6aed7eb35c14ad6000057
name Michael Lovesllamas Lankford
laptop pc
isSuperUser false
isDJ false
afkWarned false
afkTime 1340089066700
droppedTime null
droppedRoom null
songCount 0
mWaitingSongLimit 0
totalSongCount 0
totalHeartCount 0
totalHeartsGiven 0
customGreeting null
bootAfterSong false
joinedTime 1340089066700
whiteList false
allowedToReserveSpot true