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();
    });

  

原文地址:https://www.cnblogs.com/meetrice/p/9348842.html

时间: 2024-10-09 11:21:36

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的相关文章

转载翻译文章:JavaScript Module Pattern: In-Dept

# JavaScript Module Pattern: In-Depth # 转载翻译文章:JavaScript Module Pattern: In-Depth*原文*:http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html 模块模式是一种常见的js开发编码模式.通常来说,它很好理解,但还有一些高级应用并没有得到太多关注.这篇文章,我将回顾基础知识,并介绍一些真正了不起的高级主题,其中包括一个我认为极为原始的

JavaScript Module Pattern: In-Depth

The module pattern is a common JavaScript coding pattern. It’s generally well understood, but there are a number of advanced uses that have not gotten a lot of attention. In this article, I’ll review the basics and cover some truly remarkable advance

Learning JavaScript Design Patterns The Module Pattern

The Module Pattern Modules Modules are an integral piece of any robust application's architecture and typically help in keeping the units of code for a project both cleanly separated and organized. In JavaScript, there are several options for impleme

Javascript Module 模式

Javascript Module Pattern 可以说是在Javascript代码实现过程中的最佳实践方法,能够清晰地表达Javascript面向对象的概念.其核心理念是用Javascript的“类”封装私有和公有的属性和方法.它不允许开发人员定义全局变量去“污染”全局对象.通过这种模式,可以提高Web的性能,也利于维护Javascript的代码. 在Javascript程序设计语言中,函数可以作为一个模块,在某些情况下,需要创建的是单例对象,而不是创建一个类的实例.在模块的内部,公共接口可

【JavaScript】JavaScript模块化编程 - CommonJS, AMD 和 RequireJS之间的关系

通行的Javascript模块规范共有两种:CommonJS和AMD 先说说CommonJS CommonJS - 大家是不是觉得JavaScript仅仅是一个客户端的编译语言,其实JavaScript设计之初不仅仅是针对客户端设计的语言.后来只是由于Web的迅速流行,加之Netscape和微软之间之争过早的将JavaScipt标准化.要了解详细的JS历史请查看:http://zh.wikipedia.org/zh-cn/JavaScript.过早的标准化JS就导致JS的诸多缺陷和标准类库的缺乏

JavaScript Patterns 5.4 Module Pattern

MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var uobj = MYAPP.utilities.object, ulang = MYAPP.utilities.lang, // private properties array_string = "[object Array]", ops = Object.prototype.toStr

Understanding the Module Pattern in JavaScript

Understanding the Module Pattern in JavaScript Of all the design patterns you are likely to encounter in JavaScript, the module pattern is probably the most pervasive遍布的,充斥各处的. But it can also look a little strange to developers coming from other lan

JavaScript基础对象创建模式之模块模式(Module Pattern)(025)

模块模式可以提供软件架构,为不断增长的代码提供组织形式.JavaScript没有提供package的语言表示,但我们可以通过模块模式来分解并组织 代码块,这些黑盒的代码块内的功能可以根据不断变化的软件需求而不断的被添加,替代和删除.模块模式由几种我们已经介绍过的模式共同组成: 命名空间模式 即时函数模式 私有成员与访问控制方法模式 依赖声明模式 模块模式的第一步是建立一个命名空间.首先我们用先前介绍的namespace()方法创建一个工具模块例子,这个例子模块提供一些数组功能: MYAPP.na

JavaScript HTML Handlebars Template

/********************************************************************* * JavaScript HTML Handlebars Template * 说明: * 最近在折腾PHP发现JavaScript能处理的事情远不止自己目前处理的事情, * 发现有JavaScript HTML框架,这样在GitHub上配合JSON作为配置,就可以自由 * 组合静态网站了. * * 2017-8-24 深圳 龙华樟坑村 曾剑锋 *****