0

Here is the idea: I want to be able to make it so that I can change the order in which inputs matter in a function. Lets say that the normal function looks as such:

var f = function(a,b,c,d){
    return a * b / c + d;
}

I want to be able to redefine the function, so that when a user calls f(w,x,y,z) if a variable(lets say changed) is true, it acts as if the user called f(w,z,y,x)! (not specifically in reverse order, just a different order than original function)

If I wanted to write a separate function that acted that way I could do the following:

var newF = function(a,b,c,d){
    if(changed === true){
        return f(c,d,b,a);
    }
    else{
        return f(a,b,c,d);
    }
}

and then replace all f calls with newF calls, but that is not what I am asking.

Is it possible to redefine a function with the same name as the actual function, and how that would be done? If I cannot, please explain why not.

Explanation:

The reason I want to do this is because I have an app that has a function to make everything naturally sound (like points in a line being straight), but if I switch some of the values, it creates a fun, unrealistic, situation, which I thought would be a fun option to implement. My option above is doable, and I could use it, but I was just wondering if it is possible to keep it within the same function name

Community
  • 1
  • 1
Ryan Saxe
  • 17,123
  • 23
  • 80
  • 128
  • 5
    I'm not sure I understand why you would want to do this. What do you hope to accomplish by changing the order of arguments on the fly like this? –  Jul 03 '13 at 14:44
  • 3
    Is this necessary? having a concrete function behave differently based on the value of a disconnected variable sounds like a recipe for disaster – Alex K. Jul 03 '13 at 14:45
  • 8
    Maybe instead of accepting multiple arguments, you want the function to accept one object where each property is an argument. Then the caller does not have to worry about order. – Felix Kling Jul 03 '13 at 14:45
  • 3
    What problem are you actually trying to solve by doing this, because I'm pretty sure this is a terrible idea. – Matt Burland Jul 03 '13 at 14:45
  • Possible duplicate: http://stackoverflow.com/questions/6464386/how-can-implement-polymorphism-in-javascript-jquery – go-oleg Jul 03 '13 at 14:46
  • 3
    You can backup the old function then redefine it. `var _oldF = f; var f = function(){ // call _oldF }`. – gen_Eric Jul 03 '13 at 14:46
  • Can you change the original function `f`? Or is the idea to not do that? – Matt Burland Jul 03 '13 at 14:47
  • I edited my answer to explain what prompted me to ask this. – Ryan Saxe Jul 03 '13 at 14:53
  • Well you can do the if in the `f` function, and there mix the values to the right order. – eric.itzhak Jul 03 '13 at 14:54
  • this is a general question that I would like to work across the board...with complicated functions that manipulate the inputs, that would be double the necessary lines of code – Ryan Saxe Jul 03 '13 at 14:56

5 Answers5

4

Is it possible to redefine a function with the same name as the actual function, and how that would be done?

Yes, you can just assign to its identifier again:

f = function(a,b,c,d) {
    if (changed)
        return a * d / c + b;
    else
        return a * b / c + d;
};

If you need to call the actual function because you don't know its contents exactly (but want to intercept its parameters), you can do

f = (function(oldF) {
    return function(a,b,c,d){
        if (changed)
            return oldF(c,d,b,a);
        else
            return oldF(a,b,c,d);
    };
})(f);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2
var oldF = f;

f = function( ... ){
    // do stuff
    oldF(...);
};
James Montagne
  • 77,516
  • 14
  • 110
  • 130
1

Use a factory method that returns the correct function at run-time.

Rake36
  • 992
  • 2
  • 10
  • 17
0

Okay first, as long as you have defined a function like this :

var f = function(...){...};

then you can override it easily. But when you do this :

function f(...){...}

The explanation simply put, when you declare the function with function f(...){... the compiler will first look for every declared function that way then run your code. On the other hand declaring as a variable f will store the function during runtime not during the preprocessing. Hence declaring like this :

function f(){
    console.log('a');
};
f();
function f(){
    console.log('b');
}

f();

Will output b b, fiddle While like this :

var f = function () {
    console.log('a');
};
f();
f = function () {
    console.log('b');
}

f();

Will output a b, fiddle

Now, I never put the arguments inside the exemples because JS doesn't care of the type and arguments size. The best way imo is to do like this :

var myProject = {};

myProject.fn = {

    f: function(args,options){
        //in your case while you need a,b,c,d you can use the args array to store them
        //options to define the 'changed' thing or whatever you need
        }

}
TecHunter
  • 6,091
  • 2
  • 30
  • 47
  • 2
    Um, but he has no function declaration? Btw, your second snippet does output `a b` not `a a` – Bergi Jul 03 '13 at 14:57
0

Aside from "why would you want to", you could do something like this:

var changed = false;

var f = function(a,b) {
    console.log("original function: " + a + "," + b);
}

f = (function(original) {
    return function(a,b) {
        console.log("new function");
         if (changed) {
             console.log("order changed");
             original(b,a);
         }
         else {
              original(a,b);
         }
     }
})(f);

f(1,2);
changed = true;
f(1,2);

Will output (in the console):

new function 
original function: 1,2 
new function 
order changed 
original function: 2,1 

fiddle

Matt Burland
  • 44,552
  • 18
  • 99
  • 171