本文介绍了parseInt(null,24)=== 23 ...等等,什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在弄乱parseInt来看看它如何处理尚未初始化的值,我偶然发现了这个gem.对于任何基数为24或更高的整数,下面的情况都会发生.

Alright, so I was messing around with parseInt to see how it handles values not yet initialized and I stumbled upon this gem. The below happens for any radix 24 or above.

parseInt(null, 24) === 23 // evaluates to true

我在IE,Chrome和Firefox中对其进行了测试,它们都警告为true,因此我认为它必须在规范中的某个地方.快速的Google搜索没有给我任何结果,所以我在这里,希望有人可以解释.

I tested it in IE, Chrome and Firefox and they all alert true, so I'm thinking it must be in the specification somewhere. A quick Google search didn't give me any results so here I am, hoping someone can explain.

我记得我听过克罗克福德(Crockford)的演讲,当时他说 typeof null ==="object" ,这是因为疏忽导致Object和Null在内存中或类似内容中具有几乎相同的类型标识符行,但现在找不到该视频.

I remember listening to a Crockford speech where he was saying typeof null === "object" because of an oversight causing Object and Null to have a near identical type identifier in memory or something along those lines, but I can't find that video now.

尝试一下: http://jsfiddle.net/robert/txjwP/

编辑更正:较高的基数返回不同的结果,32返回785077
编辑2 来自zzzzBov: [24 ... 30]:23,31:714695,32:785077,33:859935,34:939407,35:1023631,36:1112745

Edit Correction: a higher radix returns different results, 32 returns 785077
Edit 2 From zzzzBov: [24...30]:23, 31:714695, 32:785077, 33:859935, 34:939407, 35:1023631, 36:1112745

tl;博士

解释为什么 parseInt(null,24)=== 23 是真实的语句.

Explain why parseInt(null, 24) === 23 is a true statement.

推荐答案

正在将 null 转换为字符串"null" 并尝试进行转换.对于基数0到23,没有可以转换的数字,因此它返回 NaN .在24,第14个字母"n" 被添加到数字系统中.在31,添加了第21个字母"u" ,可以对整个字符串进行解码.在37点,不再可以生成任何有效的数字集,并且返回NaN.

It's converting null to the string "null" and trying to convert it. For radixes 0 through 23, there are no numerals it can convert, so it returns NaN. At 24, "n", the 14th letter, is added to the numeral system. At 31, "u", the 21st letter, is added and the entire string can be decoded. At 37 on there is no longer any valid numeral set that can be generated and NaN is returned.

js> parseInt(null, 36)
1112745

>>> reduce(lambda x, y: x * 36 + y, [(string.digits + string.lowercase).index(x) for x in 'null'])
1112745

这篇关于parseInt(null,24)=== 23 ...等等,什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:53