How to construct a custom object in JS

Source: Internet
Author: User

I. what the object is and how it is composed and used :

1. The object is a special data structure that combines the data with the program code that acts on the basis of the data.

2. In fact, objects are simply variables and functions that are combined into a single struct.

3. When a variable is placed in an object, it is called an attribute, and when the function is placed in an object, it is called a method.

4. When referencing attributes and methods, provide the name of the object, followed by the dot operator, followed by the name of the attribute or method.

Two. The constructor is responsible for creating the object :

Now that the object has related data, the data needs to be initialized at object creation time, and a special method-constructor (constructor) is required to prepare the object for operation. Each object requires its own constructor, with the same name as the object (the initials are capitalized, and the JS comes with the object (Date,string,array)). When you create an object, you call the constructor to initialize the object. When creating a custom object, it is our job to design the appropriate constructor.

When you create an object as a constructor, you use the new operator, which invokes the object's constructor to open the object creation process.

Three. What is inside the constructor:

The constructor creates the attributes of the object (similar to the member variable) and the initial value of the object. When creating an attribute in a constructor, you need to use the keyword this in JS, thefunction of this--to create an attribute that belongs to the "This" object, not just the local variable in the constructor.

Object attributes are created and initialized using the object annotation (dot operator) and the keyword this. Without this, the constructor does not know that you are creating an object attribute, the result of the example constructor creates 4 attributes, each of which is assigned the 4 values that are passed into the constructor as arguments.

This.date indicates that the date attribute belongs to the object where the code appears!!! (This is referenced with an object from within the object)

How to construct a custom object in JS

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.