【发布时间】:2019-04-29 02:52:34
【问题描述】:
在 C# 中有多种方法可以做到这一点C# Pass bitwise operator as parameter 特别是“Bitwise.Operator.OR”对象,但是这样的事情可以在 JavaScript 中完成吗?例如:
function check(num1, num2, op) {
return num1 op num2; //just an example of what the output should be like
}
check(1,2, >); //obviously this is a syntax error, but is there some kind of other object or bitwise operator of some kind I can plug into the place of ">" and change the source function somehow?
【问题讨论】:
-
不,没有办法。唯一的方法是传递一个包含运算符的字符串并使用
eval()这不好。 -
@MaheerAli
new Function在这里更好。 -
@Kaiido 这将如何运作?
-
@Kaiido
new Function也使用eval() -
所有位运算符都希望接受两个或仅一个参数。如果您有多个运算符,则(使用对象示例)多次引用该对象,
check['&&'](check['<'](2, 5), check['<'](8, 10))等,类似
标签: javascript bitwise-operators boolean-operations