• Javascript设计模式之创建对象的灵活性


    传统的

    /* Anim class */
    var Anim = function () {};
    Anim.prototype.start = function () {
        console.log("start");
    }
    
    Anim.prototype.stop = function () {
        console.log("stop");
    }
    
    /* Usage */
    var myAnim = new Anim();
    myAnim.start();
    myAnim.stop();
    
    

    json方式

    /* Anim class */
    var Anim = function () {};
    Anim.prototype = {
        start:function () {
            console.log("start");
        },
        stop:function () {
            console.log("stop");
        }
    }
    
    /* Usage */
    var myAnim = new Anim();
    myAnim.start();
    myAnim.stop();
    

    给Function定义一个方法

    Function.prototype.method = function (name,fn) {
        this.prototype[name] = fn;
    }
    
    var Anim = function () {};
    Anim.method('start',function(){
        console.log("start");
    });
    Anim.method('stop',function(){
        console.log("stop");
    });
    
    var myAnim = new Anim();
    myAnim.start();
    myAnim.stop();
    
    
    

    升级Function方法,可以连续使用

    Function.prototype.method = function (name,fn) {
        this.prototype[name] = fn;
        return this; // 返回对象本身
    }
    
    var Anim = function () {};
    Anim.method('start',function(){
        console.log("start");
    }).method('stop',function(){
        console.log("stop");
    });
    
    var myAnim = new Anim();
    myAnim.start();
    myAnim.stop();
    
    
    
  • 相关阅读:
    深拷贝浅拷贝
    mock demo
    django 2. 配置信息
    django 1. 入门基础
    java 12. 方法重载
    java 11. 设计规约
    java 10. 参数返回值问题
    java 9. 面向对象之方法设计
    java 8. 面向对象之属性
    java 7. 多维数组
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6196914.html
Copyright © 2020-2023  润新知