Javascript 测试用例中的虚假字符:全部中断

发布于 2024-08-14 02:11:23 字数 1568 浏览 8 评论 0原文

我正在使用名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看透却不说透 2024-08-21 02:11:23

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 is 0x16 (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 is 1.632e2 (163.2).

!! is a trick to convert to boolean. Consider !!0, which can be interpreted as !(!0). It first becomes true, which of course is not accurate, then goes back to false which is the correct boolean representation for 0.

锦欢 2024-08-21 02:11:23

“0x....”是十六进制数的 JavaScript 表示法。但是,16.0x0000 不能被解释为任何有意义的值。 typeof 16.0x00 抛出 JavaScript 错误。这是预料之中的。你真正想要的是 is_float(0x16) 或类似的东西

听起来你正在验证输入。如果您确实想测试输入的内容(例如在文本字段中)实际上是浮点数,我建议创建您自己的函数,例如:

function is_float(input) {
    if (typeof input !== 'string') {
        input = input.toString();
    }
    return /^\-?\d{1,9}\.\d+$/.test(input);
}

is_float("16.0x00"); //false
is_float("16.00");   //true

这样您就不必处理转换数字等。

"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 similar

It 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:

function is_float(input) {
    if (typeof input !== 'string') {
        input = input.toString();
    }
    return /^\-?\d{1,9}\.\d+$/.test(input);
}

is_float("16.0x00"); //false
is_float("16.00");   //true

That way you don't have to deal with converting numbers etc.

乜一 2024-08-21 02:11:23

要跳过语法错误,我认为唯一的方法是将其放入 try catch 中并像这样评估它。

try{eval("document.write(is_float(16.0x00000000));")}catch(e){}

但这有点奇怪,最好将 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.

try{eval("document.write(is_float(16.0x00000000));")}catch(e){}

But its kind of weird way, better put try catch in is_float function and accept only strings like "16.0x00000" not 16.0x00000.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文