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;
    }
    }
  • 相关阅读:
    数据包发送
    linux 进程调度3
    linux 进程调度2
    linux 进程调度1
    进程间通信:信号
    fork vfork clone学习
    跳表
    【转】Linux内存管理综述
    如何优雅的写出链表代码
    This function or variable may be unsafe Consider using xxx instead
  • 原文地址:https://www.cnblogs.com/wuzy/p/2993719.html
Copyright © 2011-2022 走看看