Review of the constructor function

Source: Internet
Author: User

What is a constructor function?

Constructors are used to assign values to initialized instantiated objects

Execution process

1. Create a new object by using the New keyword

2. The constructor assigns the newly created object to the This object within the function

3. Add members to new objects through the This keyword within a function

4. Return the newly created object by default

Precautions

1. Constructor name is best capitalized

2. Use with new

3. If you set the return value manually

A) return a basic type of data (including undefined and null), the function still returns the newly created object

b) Return an object of type data, the function will return this object

4. If you do not give constructor arguments when calling a function, the constructor will not create a new object, and this will point to the window, and the added attributes will be added to the window

What is a prototype?

When the constructor is created, the system creates a mysterious object associated with it by default, which is the prototype

The role of prototypes?

The properties and methods in the prototype can be shared by all objects instantiated by the constructor associated with it

Can expand built-in objects

How to get a prototype

constructor. prototype

Instantiates an object. __proto__

How prototypes are used

Direct Replacement prototypes

The prototype of the object created before replacing the prototype and the prototype of the object created after the substitution is not the same

Substitution is not a modification, it is a whole replacement.

Add members to the prototype using the object's dynamic properties (

(Member: A generic name for properties and methods)

. Cosntructor: The prototype function points to the constructor of the prototype

 A) When a point syntax is assigned to a property, the prototype is not searched.

b) when assigning a value using Point syntax , if the property does not exist in the object , it will be added to the object and will not be found in the prototype

Review of the constructor function

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.