zoukankan      html  css  js  c++  java
  • javascript设计模式5

    子类引用父类

    function extend(subClass,superClass){
        var F=function(){};
        F.prototype=superClass.prototype;
        subClass.prototype=new F();
        subClass.prototype.constructor=subClass;
        subClass.superClass=superClass.prototype;
        if(subClass.prototype.constructor==Object.prototype.constructor){
            superClass.prototype.constructor=superClass;
        }
    }

    原型式继承

    var Person={
        name:'default name',
        getName:function(){
            return this.name;
        }
    };

    用工厂模式

    var CompoundObject={};
    CompoundObject.string1='default value',
    CompoundObject.createChildObject=function(){
        return{
            bool:true,
            num:10
        }
    };
    CompoundObject.childObject=CompoundObject.createChildObject();
    var compoundObjectClone=clone(CompoundObject);
    compoundObjectClone.childObject=CompoundObject.createChildObject();
    compoundObjectClone.childObject.num=5;

    例子中的clone函数

    function clone(object){
        function F(){}
        F.prototype=object;
        return new F;
    }

    掺元类:通过扩充的方式共享函数

    var Mixin=function(){};
    Mixin.prototype={
        serialize:function(){
            var output=[];
            for(key in this){
                output.push(key+':'+this[key]);
            }
            return output.join(',');
        }
    }
    function augment(receivingClass,givingClass){
        if(arguments[2]){
            for(var i=2,len=arguments.length;i<len;i++){
                receivingClass.prototype[arguments[i]]=givingClass.prototype[arguments[i]];
            }
        }
        else{
            for(methodName in givingClass.prototype){
                if(!receivingClass.prototype[methodName]){
                    receivingClass.prototype[methodName]=givingClass.prototype[methodName];
                }
            }
        }
    }
  • 相关阅读:
    20180604_Myeclipse下配置SVN报错问题 svn
    20180603_升级Win10后,远程连接桌面连接,出现身份验证错误!
    20180603_navicat 连接sqlserver提示要安装 sql server native client
    VB.net程序实现分页
    多线程Demo
    多线程Demo VB.net
    SQLServer数据库子结构
    SQLServer数据库常用命令
    传播路径图调查2013年初
    拾遗
  • 原文地址:https://www.cnblogs.com/sdgjytu/p/4214336.html
Copyright © 2011-2022 走看看