JS Object-oriented inheritance-prototype inheritance

Source: Internet
Author: User

//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

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.