Search code examples
javascriptfunctionargsrenaming

Renaming a function with args without rewriting them - Javascript


in my code I have a class and I need to get a function at

this.behaviour.trembling.start(with many args...)

and create a function in my class called

startTrembling(with the same many args...)

which simply calls the first function passing the args. Something like this:

startTrembling(arg1, arg2, arg3, arg4, ...) { 
this.behaviour.trembling.start(arg1, arg2, arg3, arg4, ...); }

The issue is that if I change an argument in the first function I also have to change it in the second one. I was just wondering if there is a way to simply rename the first function, using direclty its parameters. Thanks in advance!


Solution

  • Assuming you're not using arrow functions:

    You can use spread operator over the implicit arguments object

    function startTrembling() {
      this.behaviour.trembling.start(...arguments);
    }