MV Aliasing causes "Maximum call stack exceeded" or already-declared errors

I've been smashing my head against this problem for a few days, and it's making me feel stupid, because I've been analyzing plugins from other people and I don't seem to be doing anything differently. When I try to alias and redefine the Game_Player initialize functions, it'll say "Maximum call stack exceeded," due to an infinite loop. So one of my friends suggested I just put those definitions all in brackets {} and declare the alias with let since they're one-time executions and won't refer to each other by the time the next alias is called. Then they'll fall out of scope. This works for getting rid of the errors, but I've haven't yet seen a single plugin where this was used for such a simple problem. I'm like in step one of one of the tutorials here. Why isn't this working?

Part of the issue is that I have two separate plugins I'm working on that both expand the Game_Player.prototype.initMembers function (just to add member variables to it!), but I've put two plugins in the same project that extend that exact function before, and they don't seem to cause infinite loops or tell me that there's a syntax error because the alias has already been declared. I'm trying to follow the example of this thread and this thread as well as just looking at random plugins by people who we all know know what they're doing, like Yanfly, but it is just glancing off my smooth brain. I don't get it. And why would you ever declare an alias globally as a const to begin with if everyone is using the same naming conventions for them?

These are the offending functions. I've tried every combination of let, var, const, or lack thereof. Please tell me what I'm doing wrong, or if I really do just have to put them in global brackets with a let declaration.

Code:
var _Game_Player_initMembers = Game_Player.prototype.initMembers;
Game_Player.prototype.initMembers = function() {
    _Game_Player_initMembers.call(this);

    this._speedMulti  = 1;
}

Code:
var _Game_Player_initMembers = Game_Player.prototype.initMembers;
Game_Player.prototype.initMembers = function() {
    _Game_Player_initMembers.call(this);

    this._active = true;
    this._prevRoom = $gameMap._mapId;
}
Hi there!

The problem here is that:
JavaScript:
var _Game_Player_initMembers = Game_Player.prototype.initMembers;

You need to put both of your plugins inside a scope(each plugin on your own scope) so the two variables _Game_Player_initMembers will not "conflict".

Example:
JavaScript:
(function() {

    const _Game_Player_initMembers = Game_Player.prototype.initMembers;
    Game_Player.prototype.initMembers = function() {
        _Game_Player_initMembers.call(this);
        this._speedMulti  = 1;
    }

})();

Or:
JavaScript:
{

    const _Game_Player_initMembers = Game_Player.prototype.initMembers;
    Game_Player.prototype.initMembers = function() {
        _Game_Player_initMembers.call(this);
        this._active = true;
        this._prevRoom = $gameMap._mapId;
    }

}

Also, don't use var. Use const or let. So you will know for sure that the variable didn't "escape" the plugin scope.

If you post all of your two plugins it will help more to see what you are doing ^^
Hi there!

Oh my goodness, thank you so much. I definitely got tunnel vision there. I had the body of my plugins outside of the (function() {})(); that all the plugins start with. I guess I didn't understand that this was the purpose of it. Yeah, that solved it instantly. Thank you again.

Original thread: https://forums.rpgmakerweb.com/threads/aliasing-causes-maximum-call-stack-exceeded-or-already-declared-errors.146686/