There's a similar question with a good answer; https://stackoverflow.com/a/30150587/5857473.
I wanted it to be a property of the room, so I changed the code like this:
Object.defineProperty(Source.prototype, 'memory', {
get: function() {
if(_.isUndefined(this.room.memory.sources)) {
this.room.memory.sources = {};
}
if(!_.isObject(this.room.memory.sources)) {
return undefined;
}
return this.room.memory.sources[this.id] = this.room.memory.sources[this.id] || {};
},
set: function(value) {
if(_.isUndefined(this.room.memory.sources)) {
Memory.sources = {};
}
if(!_.isObject(this.room.memory.sources)) {
throw new Error('Could not set source memory');
}
this.room.memory.sources[this.id] = value;
}
});
This avoids looping through all rooms to set up the shortcuts, etc. as above.