开发者

pass < or > operator into function as parameter?

开发者 https://www.devze.com 2022-12-16 16:24 出处:网络
Inside my function there is an if() statement like this: if(passedValue < staticValue) But I need to be able to pass a parameter dictating whether the if expression is like above or is:

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

if(passedValue < staticValue)

But I need to be able to pass a parameter dictating whether the if expression is like above or is:

if(开发者_JAVA技巧passedValue > staticValue)

But I cant really pass < or > operator in has a parameter, so I was wondering what is the best way to do this?

Also if the language I am using matters its ActionScript 3.0

Thanks!!


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

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

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

    /* ... */
}

Then to use it:

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

or:

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


Why not make the function take a bool as an argument and perform the comparison directly when calling the function?

ExampleFunction(arg1, (passedValue > staticValue))


I don't know Actionscript, but can't you make a variable called:

bool greaterThan = true;

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


You're right, you can't pass operators. You can pass a variable indicating which operator to use though.

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

精彩评论

暂无评论...
验证码 换一张
取 消