admin管理员组文章数量:1024660
In C# there are various ways to do this C# Pass bitwise operator as parameter specifically the "Bitwise.Operator.OR" object, but can something like this be done in JavaScript? For example:
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?
In C# there are various ways to do this C# Pass bitwise operator as parameter specifically the "Bitwise.Operator.OR" object, but can something like this be done in JavaScript? For example:
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?
Share
Improve this question
asked Apr 29, 2019 at 2:52
B''H Bi'ezras -- Boruch HashemB''H Bi'ezras -- Boruch Hashem
1
18
-
No there is no way. The only way is to pass a string containing operator and use
eval()
which is not good. – Maheer Ali Commented Apr 29, 2019 at 2:53 -
@MaheerAli
new Function
is better here. – Kaiido Commented Apr 29, 2019 at 2:54 - @Kaiido how would that work? – B''H Bi'ezras -- Boruch Hashem Commented Apr 29, 2019 at 2:54
-
@Kaiido
new Function
also useseval()
– Maheer Ali Commented Apr 29, 2019 at 2:54 -
1
All the bitwise operators look to accept either two or only one argument. If you have multiple operators, then (using the object example) reference that object multiple times,
check['&&'](check['<'](2, 5), check['<'](8, 10))
etc, something like that – CertainPerformance Commented Apr 29, 2019 at 3:05
4 Answers
Reset to default 6You can create a object with keys as operators and values as functions. You will need Bracket Notation to access the functions.
You can use Rest Parameters and some()
and every()
for more than two parameters for &&
,||
.
For the bitwise operator or +,-,*,/
multiple values you can use reduce()
const check = {
'>':(n1,n2) => n1 > n2,
'<':(n1,n2) => n1 < n2,
'&&':(...n) => n.every(Boolean),
'||':(...n) => n.some(Boolean),
'&':(...n) => n.slice(1).reduce((ac,a) => ac & a,n[0])
}
console.log(check['>'](4,6)) //false
console.log(check['<'](4,6)) /true
console.log(check['&&'](2 < 5, 8 < 10, 9 > 2)) //true
console.log(check['&'](5,6,7) === (5 & 6 & 7))
You can do the exact same thing suggested by the linked answers:
function check(num1, num2, op) {
return op(num1, num2);
}
// Use it like this
check(3, 7, (x, y) => x > y);
You can also create an object that provides all of these operations:
const Operators = {
LOGICAL: {
AND: (x, y) => x && y,
OR: (x, y) => x || y,
GT: (x, y) => x > y,
// ... etc. ...
},
BITWISE: {
AND: (x, y) => x & y,
OR: (x, y) => x | y,
XOR: (x, y) => x ^ y,
// ... etc. ...
}
};
// Use it like this
check(3, 5, Operators.BITWISE.AND);
How about something like:
function binaryOperation( obj1, obj2, operation ) {
return operation( obj1, obj2 );
}
function greaterThan( obj1, obj2 ) {
return obj1 > obj2 ;
}
function lessThan( obj1, obj2 ) {
return obj1 < obj2 ;
}
alert( binaryOperation( 10, 20, greaterThan ) );
alert( binaryOperation( 10, 20, lessThan ) );
It is not possible. But 1 way you can approach this is by doing the following:
function evaluate(v1, v2, op) {
let res = "" + v1 + op + v2;
return eval(res)
}
console.log(evaluate(1, 2, "+"));
# outputs 3
But be careful while passing args
as they will be evaluated which is dangerous if some hacky code is passed to the function.
In C# there are various ways to do this C# Pass bitwise operator as parameter specifically the "Bitwise.Operator.OR" object, but can something like this be done in JavaScript? For example:
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?
In C# there are various ways to do this C# Pass bitwise operator as parameter specifically the "Bitwise.Operator.OR" object, but can something like this be done in JavaScript? For example:
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?
Share
Improve this question
asked Apr 29, 2019 at 2:52
B''H Bi'ezras -- Boruch HashemB''H Bi'ezras -- Boruch Hashem
1
18
-
No there is no way. The only way is to pass a string containing operator and use
eval()
which is not good. – Maheer Ali Commented Apr 29, 2019 at 2:53 -
@MaheerAli
new Function
is better here. – Kaiido Commented Apr 29, 2019 at 2:54 - @Kaiido how would that work? – B''H Bi'ezras -- Boruch Hashem Commented Apr 29, 2019 at 2:54
-
@Kaiido
new Function
also useseval()
– Maheer Ali Commented Apr 29, 2019 at 2:54 -
1
All the bitwise operators look to accept either two or only one argument. If you have multiple operators, then (using the object example) reference that object multiple times,
check['&&'](check['<'](2, 5), check['<'](8, 10))
etc, something like that – CertainPerformance Commented Apr 29, 2019 at 3:05
4 Answers
Reset to default 6You can create a object with keys as operators and values as functions. You will need Bracket Notation to access the functions.
You can use Rest Parameters and some()
and every()
for more than two parameters for &&
,||
.
For the bitwise operator or +,-,*,/
multiple values you can use reduce()
const check = {
'>':(n1,n2) => n1 > n2,
'<':(n1,n2) => n1 < n2,
'&&':(...n) => n.every(Boolean),
'||':(...n) => n.some(Boolean),
'&':(...n) => n.slice(1).reduce((ac,a) => ac & a,n[0])
}
console.log(check['>'](4,6)) //false
console.log(check['<'](4,6)) /true
console.log(check['&&'](2 < 5, 8 < 10, 9 > 2)) //true
console.log(check['&'](5,6,7) === (5 & 6 & 7))
You can do the exact same thing suggested by the linked answers:
function check(num1, num2, op) {
return op(num1, num2);
}
// Use it like this
check(3, 7, (x, y) => x > y);
You can also create an object that provides all of these operations:
const Operators = {
LOGICAL: {
AND: (x, y) => x && y,
OR: (x, y) => x || y,
GT: (x, y) => x > y,
// ... etc. ...
},
BITWISE: {
AND: (x, y) => x & y,
OR: (x, y) => x | y,
XOR: (x, y) => x ^ y,
// ... etc. ...
}
};
// Use it like this
check(3, 5, Operators.BITWISE.AND);
How about something like:
function binaryOperation( obj1, obj2, operation ) {
return operation( obj1, obj2 );
}
function greaterThan( obj1, obj2 ) {
return obj1 > obj2 ;
}
function lessThan( obj1, obj2 ) {
return obj1 < obj2 ;
}
alert( binaryOperation( 10, 20, greaterThan ) );
alert( binaryOperation( 10, 20, lessThan ) );
It is not possible. But 1 way you can approach this is by doing the following:
function evaluate(v1, v2, op) {
let res = "" + v1 + op + v2;
return eval(res)
}
console.log(evaluate(1, 2, "+"));
# outputs 3
But be careful while passing args
as they will be evaluated which is dangerous if some hacky code is passed to the function.
本文标签: JavaScriptpass a boolean (or bitwise) operator as an argumentStack Overflow
版权声明:本文标题:JavaScript -- pass a boolean (or bitwise) operator as an argument? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745520393a2154289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论