JavaScript about inheritance

Source: Internet
Author: User

The previous article has introduced the combination of inheritance, and now we will talk about the remaining several inheritance.

prototype Inheritance

Call a function to receive the object returned by the function, and the object's prototype is the parameter object passed in to the function.

Such as:

function    personobject (o) {       function  F () {}       = 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 added a method to normalize the stereotype inheritance, this method is Object.create (), this method has two parameters, the first is as a new object prototype object, like the person above, and the second is to define additional properties for the new object. The second parameter is optional.

Such as:

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

You can use prototype inheritance when you want only one object to remain similar to another object.

Parasitic inheritance

Implement a prototype inheritance in a function, and then add your own properties and methods for the object you receive.

Such as:

function   createanother (o) {      var  person_one =  personobject (o);       function () {              alert ("HI");      }       return   Person_one;}

Parasitic combined inheritance

Combinatorial inheritance also has its drawbacks, it implements two property inheritance, and parasitic combined inheritance avoids this problem. Instances inherit properties through constructors, whereas prototypes are inherited by means of parasitic inheritance.

Such as:

function   inherit (subtype, supertype) {      var  prototype = Object (supertype.prototype);       = subtype;       = prototype;}

By invoking the above function, the prototype of the implementation Subtye.prototype is Supertype.prototype, which completes the inheritance of the prototype method.

JavaScript about 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.