var str='true && (true || false) && true';
if (str) {
console . log('true');
} else {
console . log('false');
}
检查上面的动态字符串是否为真或假的条件。
最佳答案
您可以使用eval
函数。一般来说,eval
计算以字符串表示的 JavaScript 代码。更多信息请看here .
但是,您应该注意以下说明,您将在上述链接中:
eval()
is a dangerous function, which executes the code it's passed with the privileges of the caller. If you runeval()
with a string that could > be affected by a malicious party, you may end up running malicious code on the > user's machine with the permissions of your webpage / extension.
var str='true && (true || false) && true';
if(eval(str)){
console.log('true');
}
else{
console.log('false');
}
关于javascript - 是否有任何程序可以检查以下字符串作为 if 语句中的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47942128/