The comparison will check whether obj
is a real object. It is nearly equivalent to checking for
typeof obj == "object"
Yet, that is also true
for null
and can lead to strange errors when we try to access its properties. So, instead of writing if (typeof obj == "object" && obj !== null)
it is commonly shortened to if (obj === Object(obj))
.
Also, it yields true
for function objects as well, while the typeof
test does not - but sometimes you want to allow everything that can hold properties, and someone will get angry on your lib if you forget functions.
So much about the pattern, Reid has written an excellent answer about the internals of Object
, which explains the behaviour you already described in your question.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…