//Animal Parent Class Super class varAnimal =function(name) { This. Name =name; This. SayHello =function() {alert ("HI, I am" + This. Name + "Would you like to make friends with me?" "); }; }; Animal.prototype.shout=function() {alert ( This. Name + ", being called! "); }; Animal.prototype.game=function() {alert ( This. Name + ", playing! "); }; varDog =function(name) {//this.name = name; This. Name =name; This. Shout =function()//overriding a function of the parent class{alert ( This. Name + ", Barking! "); }; }; varCat =function(name) { This. Name =name; This. Shout =function() {alert ( This. Name + ", Meow meow! "); }; }; //prototype InheritanceDog.prototype = Cat.prototype =NewAnimal (); varXH =NewDog ("Little Black"); Xh.sayhello (); Xh.shout (); Xh.game (); varXM =NewCat ("Little Mi"); Xm.sayhello (); Xm.shout (); Xm.game ();
After encapsulating a function:
varInherit =function(subclass, superclass) {Subclass.prototype=Newsuperclass ();}; //Animal Parent Class Super classvarAnimal =function(name) { This. Name =name; This. SayHello =function() {alert ("HI, I am" + This. Name + "Would you like to make friends with me?" "); };}; Animal.prototype.shout=function() {alert ( This. Name + ", being called! ");}; Animal.prototype.game=function() {alert ( This. Name + ", playing! ");}; varDog =function(name) {//this.name = name; This. Name =name; This. Shout =function()//overriding a function of the parent class{alert ( This. Name + ", Barking! "); };}; varCat =function(name) { This. Name =name; This. Shout =function() {alert ( This. Name + ", Meow meow! "); };}; //prototype Inheritance//Dog.prototype = Cat.prototype = new Animal ();Inherit (Dog, Animal); Inherit (Cat, Animal);varXH =NewDog ("Little Black"); Xh.sayhello (); Xh.shout (); Xh.game (); varXM =NewCat ("Little Mi"); Xm.sayhello (); Xm.shout (); Xm.game ();
Add a extends method to a function
Function.prototype.extends =function(superclass) { This. prototype =Newsuperclass ();}; //Animal Parent Class Super classvarAnimal =function(name) { This. Name =name; This. SayHello =function() {alert ("HI, I am" + This. Name + "Would you like to make friends with me?" "); };}; Animal.prototype.shout=function() {alert ( This. Name + ", being called! ");}; Animal.prototype.game=function() {alert ( This. Name + ", playing! ");}; varDog =function(name) { This. Name =name; This. Shout =function()//overriding a function of the parent class{alert ( This. Name + ", is barking, called very happy!" "); };};D Og.extends (Animal); varCat =function(name) { This. Name =name; This. Shout =function() {alert ( This. Name + ", Meow meow! "); };}; Cat.extends (Animal); //prototype Inheritance//Dog.prototype = Cat.prototype = new Animal ();/*Inherit (Dog, Animal); Inherit (Cat, Animal);*///dog.extends (Animal);//cat.extends (Animal); /*var xh = new Dog ("Small black"); Xh.sayhello (); Xh.shout (); Xh.game (); var xm = new Cat ("small Microphone"); Xm.sayhello (); Xm.shout (); Xm.game ();*/ varHusky =function(name, color, sex) { This. Name =name; This. color =color; This. Sex =sex; This. SayHello =function() {alert ("Hello, I am a little" + This. Sex + "dog, there's a very nice name," said the + This. Name + "", would you like to make friends with me? "); }; This. Showcolor =function() {alert ( This. color); }; /*this.shout = function ()//rewrite the parent class's functions {alert (this.name +), hum! "); };*/}; Husky.extends (DOG); varXH =NewHusky ("Xiao Ha", "Black and white", "public"); Xh.sayhello (); Xh.shout (); Xh.game (); Xh.showcolor () ;
JS Object-oriented inheritance-prototype inheritance