Do: (做:)
var isTrueSet = (myValue == 'true');
You could make it stricter by using the identity operator ( ===
), which doesn't make any implicit type conversions when the compared variables have different types, instead of the equality operator ( ==
). (您可以通过使用标识运算符( ===
)来使其更严格,当比较的变量具有不同类型时,它不会进行任何隐式类型转换,而不是等于运算符( ==
)。)
var isTrueSet = (myValue === 'true');
Don't: (别:)
You should probably be cautious about using these two methods for your specific needs: (您可能应谨慎使用这两种方法来满足您的特定需求:)
var myBool = Boolean("false"); // == true
var myBool = !!"false"; // == true
Any string which isn't the empty string will evaluate to true
by using them. (任何不是空字符串的字符串都将通过使用它们来评估为true
。) Although they're the cleanest methods I can think of concerning to boolean conversion, I think they're not what you're looking for. (尽管它们是我可以想到的与布尔转换有关的最干净的方法,但我认为它们并不是您想要的。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…