2015-02-05 90 views
3

documentation为什么使用Number.parseInt而不是parseInt()?

这种方法其行为与全局函数parseInt函数()

但是,因为它是实验性的,the compatibility是最糟糕的。例如,在IE或Safari中不可用。

那么,为什么开发人员应该使用Number.parseInt()?

+0

因为开发人员使用支持它的JS引擎? – epascarello 2015-02-05 17:25:43

+3

我不认为有更好的理由来使用它。该功能完全相同。他们只是试图删除ECMAScript 6中的全局函数,并使其成为类方法。 – 2015-02-05 17:26:36

+0

[JavaScript中parseInt(string)和Number(string)之间的区别是什么?](http://stackoverflow.com/questions/4564158/what-is-the-difference-between-parseintstring-and- numberstring-in-javascript) – 2015-02-05 17:26:45

回答

2

使用Number.parseIntparseInt()更受欢迎是因为JavaScript社区摆脱使用全局变量的趋势。在Number.parseInt Mozilla的文件指出:

...,是2015年的ECMAScript(其目的是全局的模块化)的一部分。

你有它。这是因为全球恐惧症:)