Differences between objects in JavaScript

Source: Internet
Author: User
The following section describes the differences between objects and objects in JavaScript ). I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the differences between objects and objects in JavaScript. Why is it necessary to use typeof to detect objects and return objects while using instanceof to connect objects?

This problem is very similar to the problem I encountered before. I think there are two problems to solve here. One is the mechanism of the new operator, and the other is the difference between the function keyword and the Funtion built-in object. I have read the blogs and standards of some of my predecessors. Here I will help you to summarize them.

1. new

The new operator is used to create an object instance. This object can be user-defined or built-in objects with constructor. If the constructor after the new expression does not return a reference Object (Object, String, etc.) built in JavaScript, new will create an anonymous Object and return it; if it is a built-in reference object or the original type, it will overwrite the anonymous object. (The original type of return is undefined when no return is required ). New Operator details

2. Functions and functions

The Function of ECMAScript is actually a complete object of functions. The function keyword is used to create constructors of all objects or common functions. The keyword ECMAScript defines classes and objects. The questioner var a = new function () {} Actually created an anonymous object instance using the constructor method, instead of an instance of the built-in Function of the system object. Therefore, a instanceof Function returns false and typeof returns "object ".

So when does typeof return "function? When it is really a function name.

`function a (){}//undefinedtypeof a//"function"`

Typeof and instanceof functions are completely different operators. Typeof is used to check the data type. instanceof is used to check whether a variable is an instance of an object.

The purpose of typeof is to check the data type, and its output is very definite only as follows:

• Undefined

• Object

• Boolean

• Number

• String

• Function

• Symbol (new)

The result returned by typeof is a string. As long as the check variable is an object or Null, it will return the object, which is not accurate enough, so there is instanceof.

Because the object does not exist, you will be prompted that the object is not defined. Object is an important Object in JavaScript, and other objects are based on it, including the functions you create. When creating a, you use the new Keyword, which is equivalent to a Function reference type instance. So a instanceof Object will be true.

The difference is that an Object is an object type, and "Object" is a string, which is meaningless if not defined.

The differences between objects and objects in the above JavaScript section (detailed description) are all the content shared by the editor. I hope to give you a reference and support for PHP.

For more articles on the differences between objects and objects in JavaScript, please refer to the PHP Chinese website!

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.