Can you use Alias's in Javascript?

Hello, I'm kind of new to Javascript - and in the process of trying to recreate some scripts I made for Ace from MV.

I was curious if you could use "Alias" like you can in Ruby script to sort of add code to a function without overwriting it?
you can assign the old method to a variable and then replace it:

Code:
const oldMethod = ClassName.prototype.methodName;
ClassName.prototype.methodName = function(...args) {
  oldMethod.call(this, ...args);
  // extra code here
}
Forgive me, since I'm pretty fuzzy with Javascript - I tried this:
Code:
const oldMethod = Sprite_Battler.prototype.initMembers
Sprite_Battler.prototype.SpriteIdleUpdate = function()
oldMethod.call = function() {
    this._idleEffect = 0; //Idle Effect Timer
};

It doesn't seem to work right.
You've cached the initMembers method in your oldMethod variable, but then appear to be making a new method called SpriteIdleUpdate. If that method is not called anywhere, then nothing will happen.

Also note your casing - SpriteIdleUpdate is a different method than spriteIdleUpdate.

Are you looking to add code to the initMembers method, or to an existing spriteIdleUpdate method, or make a new method called spriteIdleUpdate? The answer isn't clear from the code you've posted.

I doubt you want initMembers to be called every time an update function is called, so taking a guess that you want to add some code to initMembers, this snippet would be more appropriate:
Code:
const originalInitMembers = Sprite_Battler.prototype.initMembers;
Sprite_Battler.prototype.initMembers = function() {
  this._idleEffect = 0;
  originalInitMembers.call(this);
};

Here, the first line stores the current code of the initMembers method, unaltered. The next line then overwrites it, saying instead of doing what it originally did, do something else.

The first line of the replacement function sets the instance of the Sprite_Battler's _idleEffect property to 0. The second then calls the code from the original, unaltered function (which was stored on the first line of the snippet).

However, the stored function needs to be called with a special method available to all functions in JavaScript called "call". It accepts as its first argument the context to call the function with, then accepts arguments matching the function's parameters. Since initMembers doesn't have any parameters, we just need to pass in context.

Since we want originalInitMembers to be called with the same context as the instance of Sprite_Battler calling the new initMembers method, we can use the keyword "this" to pass it into call().

"this" and context in JavaScript doesn't work the same as it does in a lot of other languages and can be tricky to understand at first. Try reading up MDN's article on this and on the .call method.
Thanks for the in depth explanation! I managed to get it working.
I'll definitely give those articles a read when I have time.

Original thread: https://forums.rpgmakerweb.com/threads/can-you-use-aliass-in-javascript.112750/