MV How to find the parameters variable names sent with the original function?

Not soley related to RMMV or RMMZ this is just a javascript question...

I don't even really know how to properly ask this question but I'll try my best to explain my desired outcome.

Okay... so say I have a function that I'm aliasing but I don't know how many parameters it can take or the variable names that were originally given to it.

just to simplify things I'll try to make an example:

Source Function:
JavaScript:
var doStuffA = function(testA, testB, testC){
  // do stuff
};

How to find the parameters variable names sent with a function but outside of the function?
Like in string format, or an array of strings?
So like to return ['testA', 'testB', 'testC']

I'm asking this so that I can alias it and still retain the correct variable names that were passed to it without knowing what the original function used for names.

Is this possible to do?

Thanks for reading and any insight help you can provide. 🙂
Yea, if you want to extract given names for an arbitrary function's arguments then I think matching from the stringified function is the way to go. There are some points to be careful of when extracting arguments from string, though:
(If you just wanted to reference the argument values then you could go through the arguments object instead.)
Yea, if you want to extract given names for an arbitrary function's arguments then I think matching from the stringified function is the way to go. There are some points to be careful of when extracting arguments from string, though:
(If you just wanted to reference the argument values then you could go through the arguments object instead.)
Awesome thank you!
Come to think of it I think I did know that lol :blush:
I'm pretty sure I read that exact post before even and totally forgot.

Thank you again! That's a big help! ☺️

Edit:
@caethyril
Sooo what way of what is mentioned in the post would you personally use?
Seems there is a lot of different approaches.. & I can't seem find the old code I used for this.. not even sure which plugin I did anymore that utilized such tactics
(I'm so bad at keeping track of my own stuff even.. I really should get a better system for doing that)

I'm thinking this post by Lambder
Does that look like the most promising?

Although this one by humbletim seems to work great so far too.
Is there any downfall to this approach that you can see?
If not I think this will be the way I do it
That stackoverflow page has some pretty thick information for getting this info from the function, but I don't think you need to do any of that. Every function has a parameter called 'arguments' which contains an array of all of the arguments that were passed to it when it was called.

You can change the way your alias is scripted to be able to pass those arguments directly to the original function, without knowing what they were, or how many of them there were. If you need to change some of them, you can directly change arguments[x] before passing it to the original function.

JavaScript:
Test_Function = function(text, x, y, width){
    console.log(text, x, y, width)
}

var test2 = Test_Function
Test_Function = function(){
        console.log("a monkey is patched!")
        arguments[0] = arguments[0]+ "zoop"
        test2.apply(this, arguments)
}

I didn't test this very well, and I had messed around with trying to get it to work without knowing how many arguments there were a long time ago, and wasn't quite able to get it to work, but for this example it works in the console, anyway. Basically, by using function.prototype.apply instead of function.prototype.call you are allowed to pass an array of arguments to the function, instead of one by one.

We know from this example, because we wrote the original Test_Function, what the arguments are, but if the original function were obfuscated, we wouldn't know that information. But using the alias, we can change any of the arguments (and experiment a bit with trying to figure out what they are), and then pass them back to the original function, as well as add anything else we want to the function, as with any alias. As you can see, when I aliased the original function, I didn't include any arguments at all, but the aliased test2 still outputs the correct arguments to the console anyway.

The arguments array contains all of the arguments passed to the function, regardless of how many named variables that function has. Since we might not know the names of those variables in the original function, we can still reference them via the arguments array, as with the line where I add a "zoop" to the first argument.

Another neat example of using this:

The Window_Base.prototype.initialize function in MZ has a single argument, a rectangle. In MV, it took the individual values of a rectangle object (x, y, width, height). Using this sort of aliasing, you can make a patch that would make any MV plugin that tries to create a window in MZ, work without causing a crash:

JavaScript:
var window_base_init = Window_Base.prototype.initialize
Window_Base.prototype.initialize = function() {
    if (arguments.length > 1){
        //not given a rectangle
        arguments[0] = new Rectangle(arguments[0], arguments[1], arguments[2], arguments[3])
    }
    window_base_init.apply(this, arguments)
};

Edit: err, maybe not that last thing, upon testing that window_base modification, it doesn't seem to work, and I'm not entirely sure why. Upon further investigation, this will work for windows which were created using window_base as their prototype, but other windows that build on window_base (such as selectable) manually call the window they're built on top of, specifically passing only the first argument to the previous window, meaning this won't work to "auto patch" random windows in RMMV plugins for use in RMMZ
Sorry, I didn't see the edit earlier! I'm with ramza on this one: like we've mentioned, you can use arguments (or, with ES6+, rest parameters) instead:
Also, on re-reading your post, I spotted a misunderstanding:
I'm asking this so that I can alias it and still retain the correct variable names that were passed to it without knowing what the original function used for names.
[Edit: looks like ramza covered this.]

(Note: consider using Array.from to copy the arguments to an array if you're in strict mode. See page linked above for details.)

Original thread: https://forums.rpgmakerweb.com/threads/how-to-find-the-parameters-variable-names-sent-with-the-original-function.147391/