Javascript 测试用例中的虚假字符:全部中断
我正在使用名为 phpjs 的 JavaScript 库 目标是创建模仿 PHP 处理方式的函数。
这是一个特别的函数: http://phpjs.org/functions/is_float:442
我已经编写了几个测试用例,一切都符合预期。然而,当我尝试这样做时,一切都中断了:
document.write(is_float(16.0x00000000));
屏幕上没有显示真或假,只有空白区域。这是为什么呢?
当我执行此操作时,在该函数中,它显示 return !!(mixed_var % 1);
什么叫双!!为了?我以前从未遇到过这种情况。在源代码中保留一个,对于以下测试用例会得到完全相同的结果。有什么情况我可能会忘记吗?
document.write(is_float(186.31));document.write('<br>');
document.write(is_float("186.31"));document.write('<br>');
document.write(is_float("number"));document.write('<br>');
document.write(is_float("16.0x00000000"));document.write('<br>');
document.write(is_float("16"));document.write('<br>');
document.write(is_float(16));document.write('<br>');
document.write(is_float(0));document.write('<br>');
document.write(is_float("0"));document.write('<br>');
document.write(is_float(0.0));document.write('<br>');
document.write(is_float("0.0"));document.write('<br>');
document.write(is_float("true"));document.write('<br>');
document.write(is_float(true));document.write('<br>');
document.write(is_float("false"));document.write('<br>');
document.write(is_float(false));document.write('<br>');
编辑:关于 0.0 问题,此问题无法修复。检查文档:
//1.0在被函数访问之前被简化为1,这使得 //它与PHP实现不同。不幸的是我们无法解决这个问题。
看起来这只是 JavaScript 自己做的事情。程序员对此无法控制。
I'm using a JavaScript library called phpjs
The goal is to create functions that mimic how PHP handles them.
Here's one function in particular: http://phpjs.org/functions/is_float:442
I've written a couple of test-cases and all is as expected. However, all breaks when I try this:
document.write(is_float(16.0x00000000));
No true or false hits the screen, just empty white space. Why is this?
While I'm at it, in that function, it says return !!(mixed_var % 1);
What is the double !! for? I've never encountered this before. Leaving one out in the source code gets the exact same result for the following test-cases. Any case I might be forgetting?
document.write(is_float(186.31));document.write('<br>');
document.write(is_float("186.31"));document.write('<br>');
document.write(is_float("number"));document.write('<br>');
document.write(is_float("16.0x00000000"));document.write('<br>');
document.write(is_float("16"));document.write('<br>');
document.write(is_float(16));document.write('<br>');
document.write(is_float(0));document.write('<br>');
document.write(is_float("0"));document.write('<br>');
document.write(is_float(0.0));document.write('<br>');
document.write(is_float("0.0"));document.write('<br>');
document.write(is_float("true"));document.write('<br>');
document.write(is_float(true));document.write('<br>');
document.write(is_float("false"));document.write('<br>');
document.write(is_float(false));document.write('<br>');
EDIT: about the 0.0 issue, this cannot be fixed. Check the documentation:
//1.0 is simplified to 1 before it can be accessed by the function, this makes //it different from the PHP implementation. We can't fix this unfortunately.
It appears that this is just something that JavaScript does, on it's own. The programmer has no control over this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
16.0x00000000
不是 JavaScript 中的有效数值表达式。如果您尝试表达十六进制值,则正确的表示法是0x16
(以 10 为基数的22
),并且不允许使用小数。如果您偶然尝试使用科学记数法来表示值,则正确的记数法是1.632e2
(163.2
)。!!
是转换为布尔值的技巧。考虑!!0
,它可以解释为!(!0)
。它首先变为true
,这当然是不准确的,然后返回到false
,这是0
的正确布尔表示。16.0x00000000
is not a valid numeric expression in JavaScript. If you are trying to express a hexidecimal value, the proper notation is0x16
(22
in base 10) and decimals are not allowed. If, by chance, you are trying to express a value using scientific notation, the proper notation is1.632e2
(163.2
).!!
is a trick to convert to boolean. Consider!!0
, which can be interpreted as!(!0)
. It first becomestrue
, which of course is not accurate, then goes back tofalse
which is the correct boolean representation for0
.“0x....”是十六进制数的 JavaScript 表示法。但是,16.0x0000 不能被解释为任何有意义的值。 typeof 16.0x00 抛出 JavaScript 错误。这是预料之中的。你真正想要的是
is_float(0x16)
或类似的东西听起来你正在验证输入。如果您确实想测试输入的内容(例如在文本字段中)实际上是浮点数,我建议创建您自己的函数,例如:
这样您就不必处理转换数字等。
"0x...." is javascript notation for a hexidecimal number. However, 16.0x0000 cannot be interpreted as anything meaningful. typeof 16.0x00 is throwing a Javascript error. This is expected. What you really want is
is_float(0x16)
or something similarIt sounds like you're validating input. If you truly want to test that something entered (in a text field, for example) is actually a float, I would suggest creating your own function like:
That way you don't have to deal with converting numbers etc.
要跳过语法错误,我认为唯一的方法是将其放入 try catch 中并像这样评估它。
但这有点奇怪,最好将 try catch 放在 is_float 函数中,并只接受像“16.0x00000”这样的字符串,而不是 16.0x00000。
To skip Syntax Error, I think only way is to put it in try catch and eval it like this.
But its kind of weird way, better put try catch in is_float function and accept only strings like "16.0x00000" not 16.0x00000.