继承

<script type="text/javascript">
    /*****************
    *    1.1函数继承
    *****************/
        //一、函数继承
        function extend(Child,Parent){

            var F = function(){};
            F.prototype = Parent.prototype;
            Child.prototype = new F();
            Child.prototype.constructor = Child;
            Child.uber = Parent.prototype;

        }

        //二、函数继承(拷贝)  -->   只针对基本数据类型
        function extend2(Child,Parent){

            var p = Parent.prototype;
            var c = Child.prototype;

            for(var i in p){
                c[i] = p[i];
            }

            c.uber = p;
        }



    /*****************
    *    1.2对象继承
    *****************/
        //一、(浅)拷贝
    function extendCopy(p){

        var c = {};
        for(var i in p){
            c[i] = p[i];
        }

        c.uber = p;

        return c;
    }

        //二、(深)拷贝
    function deepCopy(p,c){

        var c = c || {};
        for(var i in p){

            if(p.hasOwnProperty(i)){
                if( typeof p[i] === "object" ){
                    c[i] = Array.isArray(p[i]) ? [] : {};
                    deepCopy(p[i],c[i]);
                }else{
                    c[i] = p[i];
                }
            }

        }
        return c;
    }

      //polyfill ES3
    if(typeof Array.isArray !== "function"){
        Array.isArray = function(candidate){
            return Object.prototype.toString.call(candidate) === '[objct Array]';
        }
    }

      //三、object() [原型继承]    -->  被es5采纳   更名为:Object.create();
    function object(o){
        var n;
        var F = function(){};
        F.prototype = o;
        n = new F();
        n.uber = o;
        return n;
    }


    /*****************
    *    2.原型继承、属性拷贝混合
    *****************/
    function objectPlus(o,stuff){
        var n;
        var F = function(){};
        F.prototype = o;
        n = new F();
        n.uber = o;

        for(var i in stuff){
            n[i] = stuff[i];
        }

        return n;
    }


    /*****************
    *    3.多重继承
    *****************/
    function multi(){

        var n = {},stuff,j = 0, len = arguments.length;
        for(j = 0; j < len; j++){
            stuff = arguments[j];
            for(var i in stuff){
                if(stuff.hasOwnProperty(i)){
                    n[i] = stuff[i];
                }
            }
        }

        return n;
    }

    /*****************
    *    4.寄生式继承
    *****************/
    function triangle(s,h){
        //【继承】object -->  之前的原型继承函数,twoDee定义的一个对象
        var that = object(twoDee);
        //【】
        that.name = 'triangle';
        that.getArea = function(){
            return this.side*this.height;
        };
        that.side = s;
        that.height = h;
        return that;
    }


    /*****************
    *    5.构造器借用 (会出现2次继承)
    *****************/
    function Shape(id){
        this.id = id;
    }
    Shape.prototype.name = 'shape';
    Shape.prototype.toString = function(){
        return this.name;
    }

    function Triangle(){
        Shape.apply(this,arguments);
    }
    //继承Shape的原型
    Triangle.prototype = new Shape(101);
    Triangle.prototype.name = 'Triangle';

    var t = new Triangle(202);


    //解决方案: 在 Triangle.prototype继承Shape时,不适用new;适用复制 -->  extend 或者  extend2
    function Shape(id){
        this.id = id;
    }
    Shape.prototype.name = 'shape';
    Shape.prototype.toString = function(){
        return this.name;
    }

    function Triangle(){
        Shape.apply(this,arguments);
    }
    //继承Shape的原型
    extend(Triangle,Shape);
    Triangle.prototype.name = 'Triangle';
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值