zoukankan      html  css  js  c++  java
  • Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br

    /**
     * Created with JetBrains PhpStorm.
     * User: scotty
     * Date: 28/08/2013
     * Time: 19:39
     */
    ;(function(global){
      "use strict";
    
      var M = function() {
    
        // Constructor. arguments are passed
        // from Module() call. this refers to m.
        function init() {
          meth_priv2();
          m.meth_pub2();
        }
    
        // Empty object, to be populated with
        // public properties and methods.
        var m = {};
    
        // Private properties. define using var x.
        // closure keeps them private.
        var prop_priv = "private property";
    
        // public properties. define using m.x.
        m.prop_pub = "public property";
    
        // private methods. define as var x = function() {}
        // closure keeps them private.
        var meth_priv = function() {
          console.log(prop_priv);
          console.log(m.prop_pub);
        };
    
        var meth_priv2 = function() {
    
          // access another priv method
          meth_priv();
    
          // access a pub method
          m.meth_pub();
        };
    
        // public methods. define as m.x = function() {}
        // private props/methods accessed via x.
        // public props/methods accessed via m.x
        m.meth_pub = function() {
          console.log(prop_priv);
          console.log(m.prop_pub);
        };
    
        m.meth_pub2 = function() {
    
          // access a priv method
          meth_priv();
    
          // access another pub method
          m.meth_pub();
        };
    
        // call the constructor
        init.apply(m, arguments);
        return m;
      };
    
      // determine correct export method depending upon
      // environment that this script was loaded in:
      if (typeof module != 'undefined' && module.exports) {
        module.exports = M; // Node / CommonJS...
      } else if (typeof define === 'function' && define.amd) {
        define('Module', [], M); // or RequireJS / AMD...
      } else {
        global.Module = M; // or browser
      }
    global.m=new M();
    })(this.window || global);
    

      使用:

        require(['m'],function(){
             m.meth_pub();
        });
    

      

     
    ;(function(global){
      "use strict";
     
      var M = function() {
     
        //构建函数
        function init() {
          meth_priv2();
          m.meth_pub2();
        }
         var m = {};
        //私有变量
        var prop_priv = "private property";
        //公有属性
        m.prop_pub = "public property";
     
        //私有函数
        var meth_priv = function() {
          console.log(prop_priv);
          console.log(m.prop_pub);
        };
     
        var meth_priv2 = function() {
          //访问另一私有方法
          meth_priv();
          //访问公有方法
          m.meth_pub();
        };
     
        //公有方法
        m.meth_pub = function() {
          console.log(prop_priv);//访问私有方法
          console.log(m.prop_pub);//访问公有方法
        };
     
        m.meth_pub2 = function() {
          //访问私有方法
          meth_priv();
          //访问公有方法
          m.meth_pub();
        };
     
        // 调用构建函数
        init.apply(m, arguments);
        return m;
      };
     
    
      if (typeof module != 'undefined' && module.exports) {
        module.exports = M; // Node / CommonJS...
      } else if (typeof define === 'function' && define.amd) {
        define('Module', [], M); // or RequireJS / AMD...
      } else {
        global.Module = M; // or browser
      }
      global.m=new M();
    
    })(this.window || global);
    

      

  • 相关阅读:
    [导入]微软轻量级“代码生成器”—Repository Factory使用(上)
    数据结构练习(41)数组中三个只出现一次的数字
    数据结构练习(43)字符串的组合
    数据结构练习(37)复杂链表的复制
    数据结构练习(36)二叉树两结点的最低共同父结点
    数据结构练习(34)对称子字符串的最大长度
    数据结构练习(38)树的子结构
    数据结构练习(39)二叉树前中后序遍历的非递归实现
    数据结构练习(42)八皇后问题
    数据结构练习(35)数组中出现次数超过一半的数字
  • 原文地址:https://www.cnblogs.com/meetrice/p/9348842.html
Copyright © 2011-2022 走看看