JS inheritance-prototype applications

Source: Internet
Author: User

JS inheritance-prototype applications

We know that JS is a scripting language based on object programming. In JavaScript, based on the principle that everything is an object, objects also involve inheritance, however, the inheritance here is different from the inheritance we have learned in the past. It uses the object prototype to construct a prototype chain to realize the inheritance of super-class objects.

1. How to Implement Object Inheritance

Function Box () {// Box structure, superclass object this. name = 'lil';} Desk. prototype = new Box (); // subclass inherits Desk, inherits Box, and forms a chain through prototype

Inheritance principle Summary:

1. Inheritance through prototype chain: object instances after super-type instantiationPrototype attribute assigned to the subclass object.

2. new Box () transfers the information in the constructor and prototype to the subclass object, that isInherit All Information.

3. Use instanceof to determine whether the subclass belongs to a super class.

4. When subclass is inherited,Prioritize attribute values in an instance(If the prototype conflicts with the constructor instance ).

2. Inheritance Issues

There is another legacy problem in the previous blog. Prototype function sharing cannot be rewritten. in inheritance, it is also shared and cannot be rewritten. At the same time, sub-classes cannot pass parameters to super classes. For this problem, JavaScript can use object impersonating to solve it.

Function Box (age) {this. name = ['lil', 'jack', 'Hello'] this. age = age;} function Desk (age) {Box. call (this, age); // impersonate an object and pass the parameter to the super-type.} var desk = new Desk (200); alert (desk. age );
Object impersonating SWOT analysis:

1. Implement parameter passing for a subclass of a super-Class Object

2,Only information in the constructor can be impersonate, not information in the prototype.

Of course, we can write all the information into the constructor, but this consumes too much memory space. Therefore, we can use the prototype chain + constructor method to implement it here, that is, the combination mode:

Function Box (age) {this. name = ['lil', 'jack', 'Hello'] this. age = age;} Box. prototype. run = function () {// write the method into the prototype and return this. name + this. age ;}; function Desk (age) {Box. call (this, age); // object impersonate, implement parameter passing, second call} Desk. prototype = new Box (); // prototype chain inheritance, inheriting all information. The first time you call a super-type var desk = new Desk (100); alert (desk. run ());

At the same time, you can also use methods such as original type inheritance to implement object parameter passing. You only need a transit function to help return an object after instantiation.

Function obj (o) {// pass a literal function F () {} // create a constructor F. prototype = o; // assign the literal function to the prototype return new F () of the constructor (); // finally return the instantiated constructor} var box = {// literal Object name: 'lil', arr: ['Elder Brother', 'sister ', 'sister']}; box1.name = 'jack'; alert (box1.name );
However, in the original type inheritance, the constructor in the intermediate function uses the prototype chain inheritance, so the reference type sharing problem occurs. Therefore, this method is usually not used.

In summary: JS inheritance is in progressGenerally, combined inheritance can be used to implement super-class parameter passing and rewriting.Sometimes, to better encapsulate and solve the problem of super-type calling in a combination, parasitic combination inheritance is also used. Here, a transit and parasitic function is required. This remains to be studied in depth...

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.