Implementation of JavaScript inheritance and analysis of pros and cons

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/shamoyuu/p/4770235.html

First, the implementation of the Inheritance Method 1, the prototype chain inheritance

This inheritance is the simplest, its implementation principle is that each AO object has a prototype, return the object type prototype reference, so you can assign a value to an object, you can implement simple prototype chain inheritance. (Forgive me the ability to express is not strong, the following see the code will be more clear)

function Animal () {    thisfunction()    {        alert ("I will Eat");}    } function Bird () {    thisfunction()    {        alert ("I Will Fly");     New Animal (); var New Bird ();p igeon.fly ();p igeon.eat ();

As a result, the characteristics of birds ' inherited animals ' eating are realized. Print Console.info (pigeon) we can see:

The __proto__ property of the current object is a animal object, and the Eat method in this animal object is its parent class, if a property or method can not be found in the current object, it will follow the prototype chain step-by-step look up.

Here bird's parent class is Animal,animal's parent class is object, or all objects that do not directly specify prototype, whose parent class is object. Because the ToString () method is in object, all objects can call it. The parent class of object is null.

Note: Prototype simple is the constructor prototype, and __proto__ is the object prototype, the specific difference can be Baidu a bit, here is not discussed in detail, after all, is not the focus.

2, the prototype pseudo-inheritance

The archetype of the prototype is: Take the parent class's constructor and execute it over again. Here's a look at the code:

functionAnimal () { This. Eat =function() {alert ("I'll eat."); }}functionBird () { This. Parent =Animal;  This. parent.apply ( This, arguments); //Delete parent that is no longer meaningful    Delete  This. Parent;  This. Fly =function() {alert ("I Can fly."); }}varPigeon =NewBird ();p igeon.fly ();p igeon.eat ();

The effect is the same as above.

3. Copy Inheritance

The principle of copy inheritance is to copy all the properties and methods of the parent class. See the code below.

functionAnimal () { This. Eat =function() {alert ("I'll eat."); }}functionBird () { This. Fly =function() {alert ("I Can fly."); }    //here is an inherited method to copy the properties or methods of all the parent classes     This. Extend =function(parent) { for(varKeyinchparent) {             This[Key] =Parent[key]; }    }}varPigeon =NewBird ();//methods for performing inheritancePigeon.extend (NewAnimal ());p igeon.fly ();p igeon.eat ();

This is the same as above.

Second, the pros and cons of the analysis (well actually just look at their shortcomings) 1, prototype chain inheritance pros and cons

1, only single inheritance. 2, after inheritance will affect all downstream objects. 3, the speed is slightly slow.

2, prototype impersonation inheritance pros and cons

1, although can be multiple inheritance, but cannot be run at the time of the dynamic inheritance, can only modify the parent class constructor.

3. Copy Inheritance (recommended)

1, none.

Because the above two has the disadvantage that it is good to avoid, it can achieve multiple inheritance, inheritance only affects the current object, and fast, do not have to modify the parent class constructor and so on, so the most recommended is this way of inheritance.

Note: The inheritance of jquery is also implemented by copy inheritance, but jquery adds a lot of validation judgments, but the principle is the same.

Iii. Summary

Now is late at night, feel the brain, lied, summed up a word, inherit or copy the inheritance of good.

Implementation of JavaScript inheritance and analysis of pros and cons

Related Article

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.