JavaScript 类型
根据 http://www.ecma-international.org /publications/files/ECMA-ST/ECMA-262.pdf, JavaScript 有 6 种类型:undefined
、null
、boolean
、string
、number
和对象
。
var und;
console.log(typeof und); // <-- undefined
var n = null;
console.log(typeof n); // <--- **object**!
var b = true;
console.log(typeof b); // <-- boolean
var str = "myString"
console.log(typeof str); // <-- string
var int = 10;
console.log(typeof int); // <-- number
var obj = {}
console.log(typeof obj); // <-- object
问题1:
为什么object
类型是null而不是null
?
问题2:
函数呢?
var f = function() {};
console.log(typeof f); // <-- function
变量f
的类型为function
。为何规范中没有明确说明 作为一个单独的类型?
谢谢,
As per http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf,
JavaScript has 6 types: undefined
, null
, boolean
, string
, number
and object
.
var und;
console.log(typeof und); // <-- undefined
var n = null;
console.log(typeof n); // <--- **object**!
var b = true;
console.log(typeof b); // <-- boolean
var str = "myString"
console.log(typeof str); // <-- string
var int = 10;
console.log(typeof int); // <-- number
var obj = {}
console.log(typeof obj); // <-- object
Question 1:
Why is null of type object
instead of null
?
Question 2:
What about functions?
var f = function() {};
console.log(typeof f); // <-- function
Variable f
has type of function
. Why isn't it specified in the specification
as a separate type?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
关于
typeof null == 'object'
,这是一个从早期就出现的错误,不幸的是这个错误将伴随我们很长一段时间,它是为时已晚在ECMAScript 5th中修复版本规范。关于函数,它们只是对象,但它们有一个特殊的内部属性,名为
[[Call]]
在调用函数时在内部使用。typeof
运算符区分普通对象并且只需检查对象是否具有此内部属性即可发挥作用。About
typeof null == 'object'
, this is a mistake that comes since the early days, and unfortunately this mistake will stay with us for a long time, it was too late to be fixed in the ECMAScript 5th Edition Specification.About the functions, they are just objects, but they have an special internal property named
[[Call]]
which is used internally when a function is invoked.The
typeof
operator distinguish between plain objects and functions just by checking if the object has this internal property.这是因为
typeof
被定义为如果输入为null
则返回“object”,如果输入可调用则返回“function”。 (参见 11.4.3typeof
运算符。)我不知道为什么标准是这样定义的(克罗克福德说这是错误的)。也许向后兼容。
It's because
typeof
is defined to return "object" if the input isnull
, and return "function" if the input is callable. (See 11.4.3 Thetypeof
Operator.)I don't know why the standard is defined like this (and Crockford said it's wrong). Maybe backward compatibility.
问题 1 的回答:
如果属性没有定义,则该属性是未定义的。 null 是一个对象的原因是,属性可以在没有值的情况下存在,但仍然有定义。
Answer to Question 1:
A property, when it has no definition, is undefined. The reason null is an Object is so that a property can exist with no value yet still have a definition.
typeof null === "object"
因为规范是这么说的,但这是 JavaScript 第一个版本中的错误。 (正如 KennyTM 上面所说)。typeof f === "function"
因为,如果没有try
/catch
,就没有其他可靠、万无一失的方法来确定某些东西是否是可调用。使用f.constructor === Function
可能 有效,但我认为不能保证一定如此。typeof null === "object"
because the spec says so, but this is a mistake from the very first version of JavaScript. (as KennyTM says above).typeof f === "function"
because, without atry
/catch
, there is no other reliable, foolproof way to determine if something is callable. Usingf.constructor === Function
might work, but I think it's not guaranteed to be so.为了完整起见,请注意,当前检查类型信息的最佳实践方法是这样的:
这会为您提供一个看起来像“[object Something]”的字符串,其中“Something”是类型名称。
For completeness, note that the current best-practice way to check type information is something like this:
That gives you a string that looks like "[object Something]", where "Something" is a type name.
null 是一个特殊值 - 它不是 false、不是 0、空字符串、NaN 或未定义。
null 是当你寻找一个不存在的对象时得到的结果 -
不是对象的未定义属性,而是事物本身。
具有一个 textNode 的段落将为节点 nextSibling 返回 null,
不匹配的正则表达式返回 null 而不是数组
等等。
也许它应该有自己的类型,
但随后它开始成为某种东西,
有类型的东西,而不是没有对象。
null is a special value- it is not false, it is not 0, or the empty string or NaN or undefined.
null is what you get when you look for an object that is not there-
not an undefined property of an object, but the thing itself.
a paragraph with one textNode will return null for the nodes nextSibling,
a regexp that does'n match returns null instead of the array
and so on.
maybe it should have its own type,
but then it starts to be something,
a something with a type, instead of the absence of an object.
还有 Array.prototype 。
Crockford 表示不要使用:
There is also Array.prototype as well.
Crockford says not to use: