我的函数中有一个像这样的if()
语句:
if(passedValue < staticValue)
但我需要能够传递参数来决定if表达式是否像上面那样,还是像下面那样:
if(passedValue > staticValue)
但是我实际上无法通过作为参数的 <
或 >
运算符,因此我想知道最好的方法是什么?
另外,如果使用的语言很重要,那么它是 ActionScript 3.0。
谢谢!
我的函数中有一个像这样的if()
语句:
if(passedValue < staticValue)
但我需要能够传递参数来决定if表达式是否像上面那样,还是像下面那样:
if(passedValue > staticValue)
但是我实际上无法通过作为参数的 <
或 >
运算符,因此我想知道最好的方法是什么?
另外,如果使用的语言很重要,那么它是 ActionScript 3.0。
谢谢!
为什么不传递一个自定义比较函数,而不是像AS3中无法传递运算符?
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;
});
bool
参数作为输入,并在调用函数时直接进行比较呢?ExampleFunction(arg1, (passedValue > staticValue))
您说得对,不能传递操作符。但是可以通过传递一个变量来指定要使用的操作符。
lessThan = true;
func(passedValue, lessThan);
我不懂Actionscript,但你不能创建一个名为:
bool greaterThan = true;
如果它是真的,就执行 >,如果是假的就执行 < 吗?