JavaScript 类型

发布于 2024-08-26 23:30:48 字数 1014 浏览 14 评论 0原文

根据 http://www.ecma-international.org /publications/files/ECMA-ST/ECMA-262.pdf, JavaScript 有 6 种类型:undefinednullbooleanstringnumber对象

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

赠意 2024-09-02 23:30:48

关于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.

段念尘 2024-09-02 23:30:48

这是因为 typeof 被定义为如果输入为 null 则返回“object”,如果输入可调用则返回“function”。 (参见 11.4.3 typeof 运算符。)

我不知道为什么标准是这样定义的(克罗克福德说这是错误的)。也许向后兼容。

It's because typeof is defined to return "object" if the input is null, and return "function" if the input is callable. (See 11.4.3 The typeof Operator.)

I don't know why the standard is defined like this (and Crockford said it's wrong). Maybe backward compatibility.

Smile简单爱 2024-09-02 23:30:48

问题 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.

草莓酥 2024-09-02 23:30:48

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 a try/catch, there is no other reliable, foolproof way to determine if something is callable. Using f.constructor === Function might work, but I think it's not guaranteed to be so.

笨死的猪 2024-09-02 23:30:48

为了完整起见,请注意,当前检查类型信息的最佳实践方法是这样的:

var typeInfo = Object.prototype.toString.call(yourObject);

这会为您提供一个看起来像“[object Something]”的字符串,其中“Something”是类型名称。

For completeness, note that the current best-practice way to check type information is something like this:

var typeInfo = Object.prototype.toString.call(yourObject);

That gives you a string that looks like "[object Something]", where "Something" is a type name.

原谅我要高飞 2024-09-02 23:30:48

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.

魔法少女 2024-09-02 23:30:48

还有 Array.prototype 。

  • Object.prototype
  • Array.prototype
  • Function.prototype
  • Number.prototype
  • String.prototype
  • Boolean.prototype

Crockford 表示不要使用:

  • 新号码( )
  • 新字符串()
  • 新布尔值()

There is also Array.prototype as well.

  • Object.prototype
  • Array.prototype
  • Function.prototype
  • Number.prototype
  • String.prototype
  • Boolean.prototype

Crockford says not to use:

  • new Number()
  • new String()
  • new Boolean()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文