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

typeof null 的结果是什么,为什么? #432

Open
yanlele opened this issue Jun 8, 2023 · 0 comments
Open

typeof null 的结果是什么,为什么? #432

yanlele opened this issue Jun 8, 2023 · 0 comments
Labels
JavaScript JavaScript 语法部分
Milestone

Comments

@yanlele
Copy link
Member

yanlele commented Jun 8, 2023

typeof null 的结果是 "object"

这是 JavaScript 中的一个历史遗留问题。在 JavaScript 最初的实现中,将 JavaScript 的值分为了几种类型,其中 null 被当作一个空对象指针。为了与其他对象类型区分开,typeof null 返回了 "object"

实际上,null 是一个表示空值的特殊值,它不是对象,也不是任何对象的实例。虽然 typeof null 返回了 "object",但这并不表示 null 是对象的一种类型。

由于这个历史遗留问题,判断一个值是否为 null 通常需要使用 value === null 进行比较,而不是依赖 typeof 运算符的结果。

@yanlele yanlele added the JavaScript JavaScript 语法部分 label Jun 8, 2023
@yanlele yanlele added this to the milestone Jun 8, 2023
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