Javascript combination inherits prototype chain inheritance parasitic inheritance

Source: Internet
Author: User

JavaScript inheritance usually has three ways.

The first type: combined inheritance :

function Supertype (name) { This. Name =name;  This. colors = ["Red","Blue","Green"]; } SuperType.prototype.sayName=function () {Console.log ( This. Name);    }; Function subtype (name, age) {//invoke the Supertype constructor by calling () to inherit the Supertype propertySupertype.call ( This, name);//second call to Supertype ()         This. Age =Age ; } Subtype.prototype=NewSupertype ();//First time CallSubType.prototype.sayAge =function () {Console.log ( This. Age);    }; varInstancel =NewSubtype ("Nicholas", A); Supertype ()

The inheritance inherits the method of the prototype chain through the constructor and the properties of the parent class, but the method will have two calls to the parent class, first in inheriting the prototype chain, and the second in inheriting the property.

The second type: prototype chain inheritance

//prototype Inheritance Instance code:    functionCreateobj (o) {//performs a shallow copy of the incoming object        functionF () {} F.prototype=o; return NewF (); }    varperson ={name:"Tom", friends: ["One", "one", "Van"]    }; varHups =createobj (person); Hups.name= "GRE"; HuPs.friends.push ("Rob."); varYeps =createobj (person); Yeps.name= "Lin"; YePs.friends.push ("Sari"); Console.log (person.friends);//"One,two,van,rob,sari"

This is nothing, JS's prototype inheritance feature.

The third type: parasitic inheritance

In the first method, when we first call the parent class, that is, when we inherit the prototype, we actually need a prototype copy of the parent class, and then we get the copy, which saves the call.

The inheritance technique is the most common.

functionInheritprototype (subtype, supertype) {varPrototype = object (Supertype.prototype);//create an object hyper-type prototype copyPrototype.constructor = subtype;//Enhanced objects Add construct properties to replicasSubtype.prototype = prototype;//Specifying Objects    }    functionsupertype (name) { This. Name =name;  This. colors = ["Red", "Blue", "green"]; } SuperType.prototype.sayName=function() {Console.log ( This. Name);    }; functionsubtype (name, age) {Supertype.call ( This, name);  This. Age =Age ;    } inheritprototype (subtype, supertype); SubType.prototype.sayAge=function() {Console.log ( This. Age); };

This article is referenced from https://my.oschina.net/quidditch/blog/307551

Javascript combination inherits prototype chain inheritance parasitic 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.