JavaScript Advanced Programming Notes--About inheritance

Source: Internet
Author: User
Tags shallow copy

Inheritance is a "wonderful" existence in JavaScript because it does not have the concept of Class (ES5), so it is only possible to emulate inherited behavior in other ways (prototype chains, constructors, object instances). Since it is a simulation, it should be a way to achieve the characteristics of inheritance, the individual believes that the core of inheritance is: reuse.

Write in front: The method is a special form of the attribute, where the property plenipotentiary is used. the properties in the function constructor are instance properties, each of which has its own copy, and the instances do not affect each other. The properties in the prototype object are shared for each instance, and once changed, the prototype properties of all instances change. The following parent class (Supertype) and subclasses (subtype) have their own instance properties and prototype properties, and their respective constructors do not repeat, listing only the core steps of the inheritance to highlight the focus.
1. Prototype chain inheritance:
1 New Supertype ();

Features: All properties of the parent class, including instance properties and prototype properties, are the prototype properties of the child class. Description: Nani?! Do you think it's a good thing to have a prototype attribute, do you consider the perception of instance properties? Since the parent is the only one for each instance, the subclass becomes shared, so the use limit is great! 2. Borrowing constructor Inheritance:Overcoming the weakness of the prototype chain: instance properties are inherited by constructors.
1 function subtype () {2 supertype.call (this); 3 }

Features: the instance property of the parent class is in the subclass or instance property, and each instance is unique to each other; You can also use the properties of the call method to pass arguments to the constructor of the parent class. Description: The problem with the prototype chain has been overcome and reused? The prototype attribute of the parent class is not reflected in the subclass at all! That's not going to work. 3. Combination Inheritance:Combine the advantages of the first two inheritance, God's God, Caesar's return to Caesar.
function subtype () {    // inherit instance properties    Supertype.call (this);} // Inheriting prototype properties New Supertype ();

Features: This is perfect, the parent class's instance attributes are inherited separately in the subclass, the prototype attributes are reused, and the advantages of passing parameters are preserved. Description: But the Virgo friends will find that the key two steps call the parent class constructor, the direct result is that the subclass inherits the parent class instance attribute to have two copies, one in the prototype object, one copy in the subclass instance, but the prototype attribute is overridden by the instance attribute. What a shameful waste it is! 4. Parasitic combined inheritance:Please don't ask me what "parasitic" means. The "waste" of the combined inheritance is mainly in the process of inheriting the prototype chain, so we do not now rewrite the subclass prototype object with the parent instance, and give the subclass a single object that includes only the parent prototype object property. First, a tool function to copy the parent prototype object:    
1 functioncopy (Uber) {2     varF =function(){};3F.prototype =Uber;4     return NewF ();5 }6 //Inheriting Instance Properties7 functionsubtype () {8Supertype.call ( This); 9 }Ten //Inheriting prototype properties OneSubtype.prototype =copy (Subtype.prototype);  ASubType.prototype.constructor = subtype;//don't forget to fix constructor

Features: the perfect implementation of the inheritance, and no waste of memory space, but also to pass parameters; it's a little tricky to write. 5. Prototype-Type InheritanceOkay, back to the parasite, let me say "prototype inheritance" before I tell the parasite:
1 function Copy (obj) {  2     varfunction() {}; 3     F.prototype = obj; 4     return New F (); 5 }

Features: This inheritance is based on an instance, just to give a parent class instance object to "also" a subclass instance object, directly bypass the constructor, very simple. Description: Rather than inheritance, this approach is more like a "shallow copy" of an object, and there are two defects: 1. There is no reuse of functions between different sub-objects, 2. Because it is a shallow copy of the properties of the object type that are shared between different sub-objects, you cannot change it arbitrarily. But the author Nicholas said yes, that's right. 6. Parasitic inheritanceParasitic inheritance is a strengthening of prototype inheritance:
1 function Createanother (original) {2     var clone = Object (original);     // you do not necessarily have to use the object method to copy the newly created original based on the 3     Clone.sayhi = Funciton () {  ...   }     // Enhanced 4     return clone; 5 }

Features: The clone returned here not only has all the properties of original, but also has its own method. Description: Parasitic combined inheritance of two defects is not repaired, so I think this inheritance is still very far-fetched, the personal feeling "parasitic" is the origin of their own sayhi on the original above, the amount of ... Back to the fourth way of inheriting, I think the place of parasitic is SubType.prototype.constructor = subtype (personal view) Summary: The most common way of inheritance should be Combined InheritanceAnd Parasitic combined inheritance。 The above is based on the JS Red Book 6.3 Inheritance chapters of some reading experience, welcome to spit Groove.

JavaScript Advanced Programming Notes--About inheritance

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.