What is the difference between object and object in JavaScript?

Source: Internet
Author: User

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 Functionthe 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"`




typeofand instanceof These two functions are completely different operators. typeofis to check the data type to instanceof see if a variable is an instance of an object.

typeofThe 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)

typeofReturns 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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.