JavaScript Object-oriented (iii)--Inheritance

Source: Internet
Author: User

1 //Inheritance: ECMAScript only supports implementing inheritance by relying on the prototype chain to implement2 //first, the prototype chain3 //first to clarify the relationship:4 //Each constructor will have a prototype object person---prototype5 //each prototype object will have a pointer to the constructor Person.prototype.constructor-- and person6 //Each instance contains an internal pointer to the prototype object Person1-prototype7 //when a prototype object equals an instance of another class, the prototype object contains a pointer to another object, and then a pointer to another constructor8 //A number of prototype objects will become a prototype chain as the layers of progression. 9 Ten //for the familiar Oo language, JS inherits from the traditional language inheritance in the concept is still relatively common One //1. It is based on inheritance on the prototype chain.  A     //to subclass. prototype Object = Parent class instance to implement inheritance relationship. Son.prototype = new Father () to implement -     //do not mix with traditional languages, traditional languages declare an object using the way the parent pointer points to the child class object -                         //JS implements the inheritance relationship by using the child class's prototype object to point to the parent class object the //2. After the subclass inherits the parent class, it will have the property of the parent class to the method. Subclass properties can override parent class properties, subclass methods can override parent class methods (pending validation) - //3. When the program searches for attribute fields, it searches from its own instance, and if it is not found, it is searched by the prototype chain in turn - //4. At the top of the prototype chain, object objects, all the prototypes of the functions are instances of object - //5. The result of calling the parent class method to the child class instance may not be the same as the results of the parent class method. The subclass inherits the methods and properties of the parent class, and can be approximated as a "stand-alone" two objects + //6. Creating a prototype object using the literal method will cut off the prototype chain.  - //7. Prototype chain problem one cannot pass parameters to the parent class constructor, so few people use the prototype chain alone + //8. Prototype chain problem two, the previous section mentions that parameters of a reference type cannot be declared in the prototype object because the parameter is shared.  A                 //However, for arguments of the reference type of the parent class declaration, the subclass inevitably inherits this parameter in the Proprtype of the subclass when inheriting . at                 //This parameter is shared by all instance objects that cause the child class.  - functionFather () { -      This. property =true; - }                     -Father.prototype.getFatherValue =function () { -     return  This. property; in } - functionSon () { to      This. Sonproperty =false; + } -Son.prototype =NewFater ();//Inheritance the //Son.prototype = {//using this form is equivalent to rewriting the prototype object of Son, which invalidates the previous sentence and cuts off the inheritance relationship * //getsonvalue:function () { $ //return this.sonproperty;Panax Notoginseng //     } - // }; the  +Son.prototype.getSonValue () =function () { A     return  This. Sonproperty; the } +Son.prototype.getFatherValue () =function () { -     return false;//overrides the method of the parent class, where the subclass object is called when it returns false, and the parent class object is called without affecting still returning true $}

JavaScript Object-oriented (iii)--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.