三元运算符改变“true”字面意义上的真实
我正在尝试使用三元运算符将字符串文字分配给变量,并在 pre
块中使用以下代码:
texta = "approve";
textd = "deny";
aAction = texta eq "approve" => "true" | "false";
dAction = textd eq "approve" => "true" | "false";
但是,这就是 JavaScript 中遇到的情况:
var texta = 'approve';
var textd = 'deny';
var aAction = true;
var dAction = false;
请注意 aAction< /code> 和 dAction 应该是字符串,但它们实际上是布尔文字。
为什么会发生这种情况?
I'm trying to assign a string literal to a variable using a ternary operator with the following code in the pre
block:
texta = "approve";
textd = "deny";
aAction = texta eq "approve" => "true" | "false";
dAction = textd eq "approve" => "true" | "false";
However, this is what comes across in the JavaScript:
var texta = 'approve';
var textd = 'deny';
var aAction = true;
var dAction = false;
Notice that aAction
and dAction
should be strings, but they are actually boolean literals.
Why is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其强制返回字符串的一种方法是使用 beesting:
没有回答为什么会发生这种情况的问题,但这是一种在这种情况下有效的 hack。
One way to force it back into a string is with a beesting:
Doesn't answer the question as to why this happens, but it's a hack that will work in this case.