javascript - Difference between Boolean object and basic type Boolean
淡淡烟草味2017-07-05 10:56:40
0
4
1160
var a=new Boolean(false); var b=false; alert(a instanceof Boolean); alert(b instanceof Boolean); The first true pops up The second one pops up false Why? I don’t quite understand.
There are two types of values in JavaScript: primitive types and reference types (objects).
false is a boolean primitive value, not an object, so false instanceof Boolean is false.
Similarly, "foo" instanceof String is also false.
Checking primitive types can be done with typeof.
You will see that the value of typeof false is "boolean", note the lowercase "b".
and:
typeof Boolean is "function" Boolean instanceof Object is true
Since JavaScript performs type conversion silently, users often ignore the differences between types. For example, var length = "hello world".length converts the original type string into an instance of the String object.
Except for object, all other types are basic types. What you are here is to determine whether it is a Boolean instance, which belongs to object. The subsequent basic type is false. If it is not a Boolean instance produced by new, the result will of course be false.
There are two types of values in JavaScript: primitive types and reference types (objects).
false
is aboolean
primitive value, not an object, sofalse instanceof Boolean
isfalse
.Similarly,
"foo" instanceof String
is alsofalse
.Checking primitive types can be done with
typeof
.You will see that the value of
typeof false
is"boolean"
, note the lowercase "b".and:
typeof Boolean
is"function"
Boolean instanceof Object
istrue
Since JavaScript performs type conversion silently, users often ignore the differences between types. For example,
var length = "hello world".length
converts the original typestring
into an instance of theString
object.instanceof is used to determine whether an object is an instance of a certain constructor.
b is obviously not an object
Except for object, all other types are basic types. What you are here is to determine whether it is a Boolean instance, which belongs to object. The subsequent basic type is false. If it is not a Boolean instance produced by new, the result will of course be false.