Concept:
1, prototype inheritance is the creation of a new type of object----subtype, the subtype is based on the parent type, the subtype has all the attributes and methods of the parent type (inherited from the parent type), and then modifies some of the content or adds new content. Inheritance is best used when a subtype model can be considered a parent type object.
2. Deriving an object type from multiple parent types is called multiple inheritance.
First, the prototype inheritance
Using the prototype property of the new keyword and constructor is a specific way of defining the type, which we have been using so far, which is good for simple objects, but this method of creating objects quickly becomes unwieldy when the program is over-using inheritance. So adding some functions as common operators can make them a bit smoother. For example, many people define inherit methods and method methods on objects.
Object.prototype.inherit=function (baseconstructor) { This.prototype = Clone (Baseconstructor.prototype); This.prototype.constructor=this; }; Object.prototype.method=function (name,func) { this.prototype[name]=func; };
With the information above, you can code like this:
function Strangearray () {} Strangearray.inherit (Array); Strangearray.method ("Push", function (value) { Array.prototype.push.call (this,value); Array.prototype.push.call (This,value); }); var strange=new strangearray (); Strange.push (4);//output [+]
Second, multiple inheritance (mixed type)
There are many ways to implement multiple inheritance, and the following is a small example that is relatively simple and applicable in most cases.
Mix-in is a special kind of prototype that can be mixed into other prototypes. Smallitem can be seen as such a prototype. By copying its method into another prototype, it also mixes itself with the prototype of the replication method.
function Mixinto (object,mixin) { Foreachin (mixin,function (name,value) { Object[name] = value; }); var Smalldetaileditem = Clone (Detaileditem); Mixinto (Smalldetaileditem,smallitem); var deadmouse = smalldetaileditem.create ("Fred the Mouse", "He is Dead"); Deadmouse.inspect (); Deadmouse.kick ();
The following example is the implementation of three inheritance.
<!DOCTYPE HTML><HTML><HeadLang= "en"> <MetaCharSet= "UTF-8"> <title></title> <Script>//The first function person (name) { This. Name=name; } Person.prototype.getName= function() { return This. Name; } functionAuthor (name, books) { This. Books=Books; } Author.prototype=NewPerson (name); Author.prototype.constructor=Author; Author.prototype.getBooks= function() { return This. Books; } varAU1=NewAuthor ("Dororo1","Learn much"); varAU2=NewAuthor ("Dororo2","Learn Less"); Alert (Au1.getname ()); Alert (Au2.getname ());//The second functionPerson (name) { This. Name=name; } Person.prototype.getName= function() { return This. Name; } functionAuthor (name, books) { This. Inherit=Person ; This. Inherit (name); This. Books=Books; } varau=NewAuthor ("Dororo","Learn much"); Alert (Au.getname ());//The thrid functionPerson (name) { This. Name=name; } Person.prototype.getName= function() { return This. Name; } functionAuthor (name, books) { This. Base= NewPerson (name); for(varKeyinch This. Base) { if(! This[key]) { This[key]= This. Base[key]; } } This. Book=Books; } varAU1=NewAuthor ("Dororo1"," Work"); varAU2=NewAuthor ("Dororo2","Play"); Alert (Au1.getname ()); Alert (Au2.getname ()); Au1.book; Au2.book; </Script></Head><Body></Body></HTML>
JS---Prototype inheritance and multiple inheritance