Aliasing methods only for a subclass

I've recently run into an issue where I want to alias a method that (sort of) doesn't exist.

I'll explain by example. Consider Game_Actor.prototype.states. If you type it into the console, it exists, but there is no declaration for it in rpg_objects.js. This is because the function is inherited from Game_BattlerBase - you can confirm this in the console by checking Game_Actor.prototype.states == Game_BattlerBase.prototype.states.

Now, I want to override the function only for actors, but in a way that another plugin wanting to do the same would still function.

The normal approach does not work correctly. For example:
Code:
// Let's call this Plugin1
var whatever1 = Game_Actor.prototype.states
Game_Actor.prototype.states = function() {
  return whatever1.call(this).concat(4,5,6);
};

The problem is far from obvious, but this will conflict with a plugin that does this:
Code:
// Let's call this Plugin2
var whatever2 = Game_BattlerBase.prototype.states
Game_BattlerBase.prototype.states = function() {
  return whatever2.call(this).concat(4,5,6);
};

If Plugin1 appears first in the plugin list, then you'll find that Plugin2 does not affect actors - it only affects enemies, because Plugin1 calls the original Game_BattlerBase.prototype.states rather than the version overridden by Plugin2. It will work if Plugin2 appears first in the plugin list (since whatever1 would now be the function defined in Plugin2), but with more than 2 plugins it could occur that every possible arrangement breaks a different thing, so I don't want to rely on plugin ordering if I can avoid it.

Has anyone else come up with a general solution to this? I'm using something like this, which I think will work correctly, but I wondered if there was a better way:
Code:
var whatever = Game_Actor.prototype.states
var is_overridden = Game_Actor.prototype.states != Game_Battler.prototype.states;
Game_Actor.prototype.states = function() {
  return (is_overridden ? whatever : Game_Battler.prototype.states).call(this).concat(4,5,6);
};

Note: Obviously the specific code in these examples won't work (as states() needs to return the state objects, not their IDs). It's just for demonstration anyway.
Rather than rewriting it for Game_Actor, couldn't you just add an if isActor() clause to the one in Game_BattlerBase?
I did consider something like this, yes:
Code:
var whatever = Game_BattlerBase.prototype.states
Game_BattlerBase.prototype.states = function() {
  var states = whatever.call(this);
  if(this instanceof Game_Actor) {
    states = states.concat(4,5,6);
  }
  return states;
};

But it seems a bit silly to be including a subclass check in the superclass method. I guess it would work, sure... any other ideas though?
Hi!
I don't know exactly what you want to do, so I have a hard time understanding.
The TLB suggestion is a safe one, I think.
But maybe you can try this?
JavaScript:
var whatever = Game_Actor.prototype.states;
Game_Actor.prototype.states = function(){
    const alias = whatever.call(this);
    // do your code
    return alias;
};
I meant use this.isActor() which is already a function in Game_BattlerBase, it's just that in the parent function it returns false. That won't be the case when it's called from an instance of Game_Actor.
@Eliaquim That's basically identical to the case I posted that doesn't work correctly if the plugin is placed before another plugin that overrides Game_BattlerBase.prototype.states.

@Trihan Hmm, okay. Still seems a little silly though, but maybe slightly less silly than using instanceof. Of course, it's also not a general solution - it won't work if I'm overriding something in Game_Party or Game_Player that that class didn't already override, for example.
@Solar_Flare oh, I'm sorry. guess I really don't understand at first what you are trying to do...
I think the best way is what @Trihan said so(I mean, the best of the best is that the other plugin don't override the Game_BattlerBase.prototype.states xD)
@Eliaquim That's basically identical to the case I posted that doesn't work correctly if the plugin is placed before another plugin that overrides Game_BattlerBase.prototype.states.

@Trihan Hmm, okay. Still seems a little silly though, but maybe slightly less silly than using instanceof. Of course, it's also not a general solution - it won't work if I'm overriding something in Game_Party or Game_Player that that class didn't already override, for example.

If you don't want to go down that route I think your best bet is using

Game_Actor.prototype.states === Game_BattlerBase.prototype.states

to check for it being previously overwritten by other plugins.
In other words... the solution I posted in the opening post.

In your opinion, which is better? The === check before aliasing, or overwrite the base class method instead and use an instanceof check (or equivalent)?
Yeah, I was agreeing with you, not stating that as if it was a new suggestion. 😛

I'd go with that method probably.

Original thread: https://forums.rpgmakerweb.com/threads/aliasing-methods-only-for-a-subclass.123612/