The instanceof of js and the problem of wrapper object, come in and have a look.

clipboard.png
Why is this result? what is the difference between the directly declared string and new"s? why is the string of s new checked with instanceof, that is, Object and String?

Apr.09,2021

what's the difference between a directly declared string and a new

one is a literal string
one is an instance of String type, and valueOf returns a literal string, so 'a'! = = new String ('a') , but 'a' = = new String ('a') , 'a' = new String ('a'). ValueOf ()

Why is the string of s new checked with instanceof , that is, Object and String ?

because String inherits from Object , String instanceof Object


remember the JS prototype chain mechanism? The string that comes out through new String inherits the String object, and String inherits the Object object, so the following s instance Object | String is true. There are two data types: value type and reference type in
JS. S created by literals is directly assigned to the value 'hello', so it belongs to the value type.' hello' is an ordinary constant that does not inherit from any object, so s instance Object is false.
note that among the underlying types, only number boolean string can be passed by value, var obj = {}; is still a reference type.

Menu