通过<或GT;运营商成函数的参数? [英] pass < or > operator into function as parameter?

查看:112
本文介绍了通过<或GT;运营商成函数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的函数有一个如果()语句:

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

if(passedValue < staticValue)

不过,我需要能够传递一个参数口述如果EX pression是否像上面或者是:

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

if(passedValue > staticValue)

但我不能真传&LT; &GT; 运营商有一个参数,所以我想知道要做到这一点,最好的方法是什么?

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

另外,如果我使用的语言的重要的ActionScript 3.0

Also if the language I am using matters its ActionScript 3.0

谢谢!

推荐答案

而不是通过运营商,这是不可能的AS3,为什么不通过自定义的比较函数?

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;
});

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

这篇关于通过&LT;或GT;运营商成函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆