(2) prototype-based class inheritance

Source: Internet
Author: User
Tags getcolor

1 version 1

var Animal = function(){        this.init(this,arguments);    };    //init    Animal.prototype.init = function(color){        this.color = color;    };    Animal.prototype.breath = function(){        console.log("breath");    };    var Dog = function(){};    Dog.prototype = new Animal();    Dog.prototype.init("white");    Dog.prototype.getColor = function(){        console.log(this.color);    };    var dog = new Dog();    dog.breath();//breath    dog.getColor();//white

(1) The Dog class inherits the animal class. This inheritance belongs to the prototype inheritance, that is, the dog prototype has an animal class instance.

 

2 version 2 class library add inheritance

var Class = function(parent){        var klass = function(){            this.init.apply(this,arguments);        };        if(parent){            var subClass = function(){};            subClass.prototype = parent.prototype;            klass.prototype = new subClass();        }        //init        klass.prototype.init = function(){};        klass.fn = klass.prototype;        klass.fn.parent = klass;        //add class property        klass.extend = function(obj){            var extended = obj.extended;            for(var i in obj){                klass[i] = obj[i];            }            if(extended) extended(klass);        };        //add prototype property        klass.include = function(obj){            var included = obj.included;            for(var i in obj){                klass.fn[i] = obj[i];            }            if(included) included(klass);        };        return klass;    };    var Animal = new Class();    Animal.include({        breath:function(){            console.log(‘breath‘);        }    });    var Cat = new Class(Animal);    var tommy = new Cat();    tommy.breath();


You can read this code carefully.

 if(parent){            var subClass = function(){};            subClass.prototype = parent.prototype;            klass.prototype = new subClass();        }

Upload the animal function again, create a local subclass prototype to point to the animal prototype, and then point the Klass prototype to the subclass instance to prevent prototype pollution.

(2) prototype-based class 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.