zoukankan      html  css  js  c++  java
  • 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();
    
    
    
  • 相关阅读:
    angluar 判断后跳转加参数
    angular 返回上一页
    angular 组件跳转组件 并传参数
    angluar 表单提交时候报错
    angular 中获取select选中的值
    javascript
    将数据渲染到页面的方式:模版
    将数据渲染到页面的几种方式
    跨域
    ajax
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6196914.html
Copyright © 2011-2022 走看看