对于js原型和原型链继承的简单理解(第三种,复制继承)

复制继承:简单理解,就是把父对象上的所有属性复制到自身对象上;

            function Cat(){
                 this.climb = function(){
                     alert("我会爬树");
                 }
             }
             
             function Dog(){
                 this.eat = function(){
                     alert("我会吃");
                 }
                 this.extend = function(parent){//extend方法实现了复制
                     for(var key in parent){
                         //console.log(key);
                         this[key] = parent[key];
                     }
                 }
             }
             var dog = new Dog();
             console.log(dog);//这里没有climb方法
             dog.extend(new Cat());//开始复制
             console.log(dog);//这里有了climb方法

这是Google下的结果

原文地址:https://www.cnblogs.com/jianjianwoshi/p/4369837.html