0

For any function, for example:

function my_function(arg){
    switch(arg){
        case 1: return 100;
        case 2: return 150;
        case 3: return 300;
    }
}

I want to add a new case 4: 500 into the function, so it should work like:

function my_function(arg){
    switch(arg){
        case 1: return 100;
        case 2: return 150;
        case 3: return 300;
        case 4: return 500;
    }
}

But I don't want overwrite the function with the same script again. So I tried this way:

function my_function(arg){
    switch(arg){
        case 4: 500;
        default: my_function(arg);
    }
}

But when I call other cases 1 2 3, the function keeps calling itself. (I understand why it happens)

Is there a way to fix this?

More generally, is there a way to overwrite a function so the new function can use the old function?

P.S. I don't want to create a function with different names that copies the old function.

Lab
  • 196
  • 3
  • 18
  • Sounds like [an XY problem](https://meta.stackexchange.com/questions/346503/what-is-the-opposite-of-the-xy-problem). Why do you need to create a function with the same name instead of updating the old function? And if you cannot update it, why not just name it differently as it's conceptually a separate one anyway? – VLAZ Aug 02 '20 at 07:44
  • Please refer to https://stackoverflow.com/a/5409468/12944233. – Hitesh A. Bosamiya Aug 02 '20 at 07:49
  • but you need a reference to the old function. – Nina Scholz Aug 02 '20 at 07:51

0 Answers0