Pass operator <or> to function as parameter?

Inside my function there is an operator if()like this:

if(passedValue < staticValue)

But I need to be able to pass a parameter that determines if the if statement has a higher or is:

if(passedValue > staticValue)

But I can’t really go through <or >operator in has a parameter, so I was wondering what is the best way to do this?

Also, if the language I use matters to ActionScript 3.0

Thank!!

+3
source share
4 answers

Instead of passing in an operator, which is not possible in AS3, why not pass in a custom comparison function?

function actualFunction(passedValue:Number, compareFunction:Function) {
    /* ... */

    if(compareFunction(passedValue, staticValue)) {
        /* ... Do something ... */
    }

    /* ... */
}

, :

actualFunction(6, function(x:Number, y:Number) {
     return x > y;
});

actualFunction(6, function(x:Number, y:Number) {
     return x < y;
});
+7

bool ?

ExampleFunction(arg1, (passedValue > staticValue))
+1

I do not know ActionScript, but you cannot create a variable called:

bool moreThan = true;

and if its true, do> if its false do <

0
source

You are right, you cannot transfer operators. You can pass a variable indicating which operator to use.

lessThan = true;
func(passedValue, lessThan); 
0
source

Source: https://habr.com/ru/post/1728511/


All Articles