zoukankan      html  css  js  c++  java
  • JavaScript继承

    1、类式继承

    //使subclass继承superclass

    function extend(subclass, superclass){
        function F() {};
        F.prototype = superclass.prototype;
        subclass.prototype = new F();//类式继承
        subclass.prototype.constructor = subclass;
        
        subclass.superclass = superclass.prototype;
        if (superclass.prototype.constructor == Object.prototype.constructor){
            superclass.prototype.constructor = superclass;
        }
    }

    2、原型式继承

    function clone(object) {

        function F() {};

        F.prototype = object.prototype;//原型继承

        return new F();

     }

    3、掺元类

    //用givingclass类来扩充receivingclass类

    function augment(receivingclass, givingclass) {

        if (arguments[2]) {

            for (var i = 2, len = arguments.length; i < len; i += 1) {

                receivingclass.prototype[arguments[i]] = givingclass.prototype[arguments[i]];

            }  

        } else {

            for (methodName in givingclass.prototype) {

                if (!receivingclass.prototype[methodName]) {

                    receivingclass.prototype[methodName] = givingclass.prototype[methodName];

                }

             }

        }

    }

    function extend(subclass, superclass){
    function F() {};
    F.prototype = superclass.prototype;
    subclass.prototype = new F();
    subclass.prototype.constructor = subclass;


    subclass.superclass = superclass.prototype;
    if (superclass.prototype.constructor == Object.prototype.constructor){
    superclass.prototype.constructor = superclass;
    }
    }function extend(subclass, superclass){
    function F() {};
    F.prototype = superclass.prototype;
    subclass.prototype = new F();
    subclass.prototype.constructor = subclass;


    subclass.superclass = superclass.prototype;
    if (superclass.prototype.constructor == Object.prototype.constructor){
    superclass.prototype.constructor = superclass;
    }
    }
    function extend(subclass, superclass){
    function F() {};
    F.prototype = superclass.prototype;
    subclass.prototype = new F();
    subclass.prototype.constructor = subclass;


    subclass.superclass = superclass.prototype;
    if (superclass.prototype.constructor == Object.prototype.constructor){
    superclass.prototype.constructor = superclass;
    }
    }
    function extend(subclass, superclass){
    function F() {};
    F.prototype = superclass.prototype;
    subclass.prototype = new F();
    subclass.prototype.constructor = subclass;


    subclass.superclass = superclass.prototype;
    if (superclass.prototype.constructor == Object.prototype.constructor){
    superclass.prototype.constructor = superclass;
    }
    }
  • 相关阅读:
    【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(二)
    手势(Gesture)的增加和识别
    [转]C#自定义开关按钮控件--附带第一个私活项目截图
    当年的试用期周工作心得
    FREESWITCH SEESION
    基于三星ARM9(S3C2410)的交通违章抓拍系统的开发
    Apache Kafka: Next Generation Distributed Messaging System---reference
    How and Why Unsafe is Used in Java---reference
    基于keepalived对redis做高可用配置---转载
    Java获取真实的IP地址--转载
  • 原文地址:https://www.cnblogs.com/wuzy/p/2993719.html
Copyright © 2011-2022 走看看