JS多继承

ES5

    //1, 单个对象的浅复制
    function extend(target,source) {
        for(var key in source){
            target[key]=source[key];
        }
        return target;
    }
    //2, 多个对象浅复制
    var mutiExtend=function () {
        //第一个参数为target, 其他都为source
        var i=1;
        var len=arguments.length;
        var target=arguments[0];
        for(;ivar source=arguments[i];
            extend(target,source);
        }
    }
    //3, 单个对象深复制
    function deepExtend(target,source) {
        for(var key in source){
            if(source[key] instanceof Object){
                if(source[key] instanceof Array){
                    target[key]=[];
                }else if(source[key] instanceof Function){
                    target[key]=function () {}
                }else{
                    target[key]={};
                }
                deepExtend(target[key],source[key]);
            }else{
                extend(target,source);
            }
        }
    }

    //4,测试单继承
     var target={"id":1};
    var source={
        name:"hyylsw",
        age:12,
        run:function () {
            console.log("run");
        },
        lovers:["mother","father","son","daughter"],
        books:{"java":["java1","java2"],"vue":["vue1","vue2","vue3"]}
    };

    deepExtend(target,source);
    source["lovers"]="me";
    source["books"]["java"][0]='java3';
    console.log(target);

    //5, 对对象的深复制
    var mutiDeepExtend=function () {
        var i=1;
        var target=arguments[0];
        var len=arguments.length;
        for(;ivar source=arguments[i];
            deepExtend(target,source);
        }
    }

ES6

//Object.assign(target,source); 浅拷贝

你可能感兴趣的:(阅读书籍总结)