Javascript: inheritance parsing and javascript Parsing

Source: Internet
Author: User

Javascript: inheritance parsing and javascript Parsing

In the previous article, we have introduced the inheritance of combinations. Now we will talk about several inheritance types.

Original Type inheritance

Call a function to receive the objects returned by this function. The prototype of this object is the parameter object of the input function.

For example:

function  personObject(o){    function F(){}    F.prototype = o;    return new F();}var person = {   name:"Nicholas",   friends:["Shelby","Court","Van"]}var person_one = personObject(person);

From the code above, we know that person is the prototype of person_one. ES5 adds a method to normalize the original type inheritance. This method is Object. create (), this method has two parameters. The first is the object as the new object prototype, like the person above, and the second is the object that defines additional attributes for the new object. The second parameter is optional.

For example:

var person_one = Object.create(person, {        name: {            value:"Jon"      }});

If you only want to keep one object similar to another, you can use the original type inheritance.

Parasitic inheritance

Implement an original type inheritance in a function, and then add your own attributes and methods for the received object.

For example:

function createAnother(o){   var person_one = personObject(o);   person_one.sayHi = function(){       alert(" hi ");   }   return person_one;}

Parasitic combined inheritance

Combination inheritance also has its disadvantages. It implements two property inheritance, and parasitic combination inheritance avoids this problem. The instance inherits attributes through constructor, while the prototype method inherits attributes through parasitic inheritance.

For example:

function inherit(subType, superType){   var prototype = Object(superType.prototype);   prototype.constructor = subType;   subType.prototype = prototype;}

By calling the above function, the prototype of subTye. prototype is superType. prototype, and the inheritance of the prototype method is completed.

The above javascript inheritance Parsing is a small part of the Content shared to you, I hope to give you a reference, but also hope you can support the house of helping customers.

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.