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;
    }
    }
  • 相关阅读:
    Selector空轮询处理(转载)
    使用SHOW binlog events查看binlog内容
    netty 3.x 实现http server和遇到的坑
    Tomcat7启动分析(三)Digester的使用(转载)
    MySQL · 引擎特性 · 基于InnoDB的物理复制实现(转载)
    InnoDB多版本(MVCC)实现简要分析(转载)
    MySQL数据库事务各隔离级别加锁情况--read committed && MVCC(转载)
    第 4 章 序列和字符串
    BLAST在Windows系统中本地化
    Sublime text3 创建html模板
  • 原文地址:https://www.cnblogs.com/wuzy/p/2993719.html
Copyright © 2011-2022 走看看