What's the difference between object and object in JavaScript, why use typeof to detect objects, return object, and use instanceof to pick up object?
————————————————————————————————————————————————————————————————————
This problem is very similar to the one I encountered before, and I think there are two problems to be solved, the other is the mechanism of operator new, and the difference between the function keyword and the funtion built-in object. Read some of the predecessors of the blog and standards, here to help the questioner summed up.
1.new
the role of the new operator is to create an object instance . This object can be user-defined, or it can be a system-brought object with constructors. If the constructor after the new expression returns not a JavaScript built-in reference object (object,string, etc.), new creates an anonymous object and returns it, or if the built-in reference object or the original type overwrites the anonymous object. (no return is actually a return primitive type undefined). The new operator describes in detail
2.function and function
the function of ECMAScript is actually a full-featured object. while function is the keyword used to create constructors for all objects, or keywords to be used by ordinary functions ECMAScript how to define classes and objects, the questioner var a=new function(){}
actually creates an instance of an anonymous object using the constructor's method, not the system built-in object Function
the instance, so a instanceof Function
returns false
, typeof
returns "object"
.
So when does typeof return to "function"? When it is really a function name.
`function a (){}//undefinedtypeof a//"function"`
typeof
and instanceof
These two functions are completely different operators. typeof
is to check the data type to instanceof
see if a variable is an instance of an object.
typeof
The purpose of this is to check the data type, and its output is very deterministic only as follows:
Undefined
Object
Boolean
Number
String
function
Symbol (NEW)
typeof
Returns the result, which is a string. As long as the variable being checked is an object, or Yes Null
, then it will return object
, which is of course not accurate enough, so there is instanceof
.
Because this object does not exist object
, you will be prompted object is not defined
. Object
It is an important object in JavaScript, and other objects are based on it, including the functions you create. When you create a
it, you use the new
keyword, which is equivalent to an Function
instance of a reference type. So a instanceof Object
it's going to be true
.
The difference is that object is an object type, and "Object" is a string that you do not define without meaning.
What is the difference between object and object in JavaScript?