typeof運算符我來到下表其狀態的對象的內部[[Class]]
屬性及其相應的值,該typeof
運營商的回報跨越。瞭解在Javascript
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
這裏要注意的一件事是typeof
正確返回Javascript中關聯的原始數據類型。
然而,它返回一個object
類型從Array.prototype
繼承的陣列,但返回function
型爲從Function.prototype
繼承的功能。由於一切都是Javascript中的對象(包括數組,函數&原始數據類型對象),我發現typeof
運算符的這種行爲非常不一致。
有人可以扔在typeof
操作在現實中是如何工作的一些輕?
您應該考慮** instanceof **處理對象時https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof – ymz
只有某些類型具有特定的結果。每個其他類型都被視爲一個對象。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof – Armin
如果typeof返回所有對象的'object',因爲所有東西都是javascript中的對象,那麼它就完全沒用了。 –