MZ My property is disappearing (solved)

I have the following three functions:

//----- Quest Hubs manages _questHubs which is a map of quest hubs -----
function QuestHubs(hubName) {
if(!this._questHubs.has(hubName)) throw new Error("Invalid hubName: " + hubName);
return this._questHubs.get(hubName);
}

QuestHubs.Initialize = function() {
if(this._questHubs) throw new Error("Duplicate _questHubs initialization.");
this._questHubs = new Map();
}

QuestHubs.Create = function(hubName) {
if(this._questHubs.has(hubName)) throw new Error("Hub name already in use: " + hubName);
this._questHubs.set(hubName, new QuestHub());
}

First I call initialize as part of a plugin command. Then, in the next plugin command on the same event I call Create.

But it it throws the error "cannot read property 'has' of undefined". Where is _questHubs going? Why did it stop existing or become inaccessible?
Your syntax looks weird to me.

The second and third functions are written as if they're methods of a QuestHubs data type.

But the first one is not, it's a freestanding function. So when you reference this._questHubs in that context, it's looking for a global variable named _questHubs, not a property of any Object you've defined.
Your syntax looks weird to me.

The second and third functions are written as if they're methods of a QuestHubs data type.

But the first one is not, it's a freestanding function. So when you reference this._questHubs in that context, it's looking for a global variable named _questHubs, not a property of any Object you've defined.

I think I figured it out... In the first case the context of this becomes local because it's actually called indirectly but in the second one the call function passes the global "this" context which overrides the normal context for this.

Turns out that this is global in the QuestHubs function itself!? So I wrote it like this:

function QuestHubs(hubName) {
if(!QuestHubs._questHubs.has(hubName)) throw new Error("Invalid hubName: " + hubName);
return QuestHubs._questHubs.get(hubName);
}
Also:
PluginManager.registerCommand(pluginName, "CreateQuestHub", args => {QuestHubs.Create.call(QuestHubs, args["hubName"]);});

Call lets you manually pass whatever you want for this.

Also, I've been using your Custom Hit Formula plugin. It is freakin' awesome!

Original thread: https://forums.rpgmakerweb.com/threads/my-property-is-disappearing-solved.167363/