Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

238.原始类型有哪几种?null 是对象嘛? #238

Open
webVueBlog opened this issue May 27, 2020 · 4 comments
Open

238.原始类型有哪几种?null 是对象嘛? #238

webVueBlog opened this issue May 27, 2020 · 4 comments
Labels
JavaScript JavaScript

Comments

@webVueBlog
Copy link
Member

[js]

@webVueBlog webVueBlog added the JavaScript JavaScript label May 27, 2020
@webVueBlog
Copy link
Member Author

在 JS 中,存在着 6 种原始值,分别是:

boolean
null
undefined
number
string
symbol
首先原始类型存储的都是值,是没有函数可以调用的,比如 undefined.toString()

@webVueBlog
Copy link
Member Author

此时你肯定会有疑问,这不对呀,明明 '1'.toString() 是可以使用的。其实在这种情况下,'1' 已经不是原始类型了,而是被强制转换成了 String 类型也就是对象类型,所以可以调用 toString 函数。

@webVueBlog
Copy link
Member Author

其中 JS 的 number 类型是浮点类型的,在使用中会遇到某些 Bug,比如 0.1 + 0.2 !== 0.3,但是这一块的内容会在进阶部分讲到。string 类型是不可变的,无论你在 string 类型上调用何种方法,都不会对值有改变。

@webVueBlog
Copy link
Member Author

另外对于 null 来说,很多人会认为他是个对象类型,其实这是错误的。虽然 typeof null 会输出 object,但是这只是 JS 存在的一个悠久 Bug。在 JS 的最初版本中使用的是 32 位系统,为了性能考虑使用低位存储变量的类型信息,000 开头代表是对象,然而 null 表示为全零,所以将它错误的判断为 object 。虽然现在的内部类型判断代码已经改变了,但是对于这个 Bug 却是一直流传下来。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript JavaScript
Projects
None yet
Development

No branches or pull requests

1 participant