New class has wrong prototype

I'm creating a new class whose data I want to access by map id and event id, similar to Game_SelfSwitches.  So I've copied a number of functions from that class and renamed them, and have created my own value and setValue functions.  I've added DataManager commands to create a new object, update save file contents, and retrieve save file contents, to include this new class.


function Game_SelfSwitches() {
this.initialize.apply(this, arguments);
}

Game_SelfSwitches.prototype.initialize = function() {
this.clear();
};

Game_SelfSwitches.prototype.clear = function() {
this._data = {};
};

// ---------------------------------------------
function Game_EventLocations() {
this.initialize.apply(this, arguments);
}

Game_EventLocations.prototype.initialize = function() {
this.clear();
}

Game_EventLocations.prototype.clear = function() {
this._data = {};
}




This is how my new class looks, compared with $gameSelfSwitches, when I start a new game:

Code:
$gameSelfSwitches
Game_SelfSwitches {_data: Object, initialize: function, clear: function, value: function, setValue: function…}
  _data: Object
  __proto__: Game_SelfSwitches
$gameEventLocations
Game_EventLocations {_data: Object, initialize: function, clear: function, value: function, setValue: function}
  _data: Object
  __proto__: Game_EventLocations



However, when I save a game and load it, I've lost the prototype and all the functions associated with the class (the data is still there and looks correct), while $gameSelfSwitches looks as it should:

Code:
$gameSelfSwitches
Object {_data: Object, @: "Game_SelfSwitches", initialize: function, clear: function, value: function…}
  @: "Game_SelfSwitches"
  _data: Object
  __proto__: Game_SelfSwitches
$gameEventLocations
Object {_data: Object, @: "Game_EventLocations"}
  @: "Game_EventLocations"
  _data: Object
  __proto__: Object



Have I done something wrong with the save/load process?


var _Shaz_REP_DataManager_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
_Shaz_REP_DataManager_createGameObjects.call(this);
$gameEventLocations = new Game_EventLocations();
};

var _Shaz_REP_DataManager_makeSaveContents = DataManager.makeSaveContents;
DataManager.makeSaveContents = function() {
var contents = _Shaz_REP_DataManager_makeSaveContents.call(this);
contents.eventLocations = $gameEventLocations;
return contents;
};

var _Shaz_REP_DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function(contents) {
_Shaz_REP_DataManager_extractSaveContents.call(this, contents);
$gameEventLocations = contents.eventLocations;
};








Edit: I think I've solved it ... the only difference between the two was that my definitions were inside the (function() {...}); block of my plugin, and Game_SelfSwitches was defined at the higher level.  When I moved my Game_EventLocations functions above that block, so they were at the same level, it worked - the save file has the correct prototype.


I don't understand the logic behind this.  Could someone please explain it to me?
Could it be because of the self invoked function rewriting things?


I recently started JS and when I was reading it mentioned that


(function () {


       stuff here


}😉


is a self invoked function that will automatically run everything within it when the script is loaded. So in this case whenever you turn on the game because PluginManager.setup is run before even Scene_Boot is.


That's my theory on it, I could be wrong.

Original thread: https://forums.rpgmakerweb.com/threads/new-class-has-wrong-prototype.71440/