简体   繁体   English

了解 Javascript 中的 typeof 运算符

[英]Understanding the typeof operator in Javascript

I came across the following table which states the internal [[Class]] property of an object and its corresponding value that the typeof operator returns.我遇到了下表,它说明了对象的内部[[Class]]属性及其typeof运算符返回的相应值。

Value               Class      Type
-------------------------------------
"foo"               String     string
new String("foo")   String     object
1.2                 Number     number
new Number(1.2)     Number     object
true                Boolean    boolean
new Boolean(true)   Boolean    object
new Date()          Date       object
new Error()         Error      object
[1,2,3]             Array      object
new Array(1, 2, 3)  Array      object
new Function("")    Function   function
/abc/g              RegExp     object (function in Nitro/V8)
new RegExp("meow")  RegExp     object (function in Nitro/V8)
{}                  Object     object
new Object()        Object     object

One thing to note here is the typeof correctly returns the primitive data types associated in Javascript.这里要注意的一件事是typeof正确返回了 Javascript 中关联的原始数据类型。

However, it returns an object type for an array which inherits from the Array.prototype , but returns a function type for a function that is inheriting from the Function.prototype .但是,它为从Array.prototype继承的数组返回一个object类型,但为从Function.prototype继承的函数返回一个function类型。

Given everything is an object in Javascript (including arrays, functions & primitive data type objects), I find this behaviour of the typeof operator very inconsistent.鉴于一切都是 Javascript 中的对象(包括数组、函数和原始数据类型对象),我发现typeof运算符的这种行为非常不一致。

Can someone throw some light on how the typeof operator works in reality?有人可以了解typeof运算符在现实中是如何工作的吗?

This is slightly odd, idiosyncratic Javascript behaviour.这有点奇怪,特殊的 Javascript 行为。 It's inherited from the earliest days of Javascript and probably would not be written in such a way today.它是从 Javascript 的早期继承而来的,今天可能不会以这种方式编写。

Nonetheless, we are where we are with Javascript, so we have to deal with it!尽管如此,我们还是使用 Javascript,所以我们必须处理它!

The thing is that values in Javascript are either objects or they are primitives.问题是 Javascript 中的值要么是对象,要么是原语。 This is a design decision.这是一个设计决定。 They cannot be anything else.它们不可能是别的东西。 The types of primitives are : 原语的类型是

  • strings字符串
  • numbers数字
  • booleans布尔值
  • symbols (from ES2015)符号(来自 ES2015)
  • the special value undefined undefined的特殊值
  • the special value null (for which typeof also returns object )特殊值nulltypeof也返回object

Anything and everything else is an object.任何事物和其他事物都是对象。 This is by design.这是设计使然。 Arrays are objects, so typeof returns object , as does every other object that is not callable (ie a function).数组是对象,因此typeof返回object ,其他所有不可调用的对象(即函数)也是如此。 See the spec for typeof .请参阅typeof的规范

The better question is to ask why you want to test if something is an array.更好的问题是问你为什么要测试某个东西是否是一个数组。 You probably don't need to, especially as array-like objects such as NodeLists are not arrays but are like them in many ways.您可能不需要这样做,尤其是像 NodeLists 这样的类数组对象不是数组,但在很多方面都与它们相似。

The best solution in most cases is to call Array.from on the value supplied: then you know that it is an array.在大多数情况下,最好的解决方案是在提供的值上调用Array.from :然后您就知道它是一个数组。

Use typeof operator in JavaScript在 JavaScript 中使用 typeof 运算符

'Typeof' is an operator which is used to return a string description of the type of a variable. 'Typeof' 是一个运算符,用于返回变量类型的字符串描述。

Example例子

console.log(typeof 42);
 output: "number"

console.log(typeof true);
 output: "boolean"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM