JS Object-Oriented inheritance

Source: Internet
Author: User
Objective

Recently saw JS object-oriented chapter, the main study of the prototype and object-oriented inheritance, in order to comb their own knowledge of the logic, hereby recorded.

JS Object-oriented

Let's talk about the JS creation object method I know now.

1. Write a function and create an object from new

2. The literal Way

The factory method may be used if you want to reuse it.

Factory methods each time you create an object and return

Construct method Create object method can be more concise

However, it is more troublesome to share common methods with implementing instances.

Prototypes can implement instances to share all properties

Each function has a pointer to the prototype object, if the prototype property of a function is assigned to an instance of another function, the prototype of the function will point to the prototype object of another function, thus forming a prototype chain.

The prototype chain can be used to simulate an object-oriented inheritance feature

In the process of prototyping, if the value of a reference type between instances points to the same reference, the values of the reference types between all instances may affect each other.

So introduce the constituent function to generate a replica object to guarantee the independence of the object their combinatorial history is called Combinatorial inheritance way

However, this pattern requires calling the parent class constructor 2 times, and performance may not be the best

At this point, the way of parasitic inheritance comes up. The way that he implements it is to create a copy of the object reference with the object () method, and then assign the replica's constructed instance to the object of the subclass, and finally assign the prototype of the subclass's object to the Copy object, so as to avoid calling the parent class constructor. The parent class is called only in the subclass constructor through the parent class. Call (This,args)

Finally, the parasitic combination inherits the perfect solution to create the object.

JS Object-Oriented inheritance

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.