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.toString;

    // private methods

    // ...

    // end var

    // optionally one-time init procedures

    // ...

    // public API

    return {

        inArray: function (needle, haystack) {

            for (var i = 0, max = haystack.length; i < max; i += 1) {

                if (haystack[i] === needle) {

                    return true;

                }

            }

        },

        isArray: function (a) {

            return ops.call(a) === array_string;

        }

        // ... more methods and properties

    };

}());

Revealing Module Pattern

Privacy pattern

The above can become:

MYAPP.utilities.array = (function () {

    // private properties

    var array_string = "[object Array]",

        ops = Object.prototype.toString,

        // private methods

        inArray = function (haystack, needle) {

            for (var i = 0, max = haystack.length; i < max; i += 1) {

                if (haystack[i] === needle) {

                    return i;

                }

            }

            return−1;

        },

        isArray = function (a) {

            return ops.call(a) === array_string;

        };

    // end var

    // revealing public API

    return {

        isArray: isArray,

        indexOf: inArray

    };

}()); 

Modules That Create Constructors

The only difference is that the immediate function that wraps the module will return a function at the end, and not an object.

MYAPP.namespace('MYAPP.utilities.Array');

MYAPP.utilities.Array = (function () {

    // dependencies

    var uobj = MYAPP.utilities.object,

        ulang = MYAPP.utilities.lang,

        // private properties and methods...

        Constr;

    // end var

    // optionally one-time init procedures

    // ...

    // public API -- constructor

    Constr = function (o) {

        this.elements = this.toArray(o);

    };

    // public API -- prototype

    Constr.prototype = {

        constructor: MYAPP.utilities.Array,

        version: "2.0",

        toArray: function (obj) {

            for (var i = 0, a = [], len = obj.length; i < len; i += 1) {

                a[i] = obj[i];

            }

            return a;

        }

    };

    // return the constructor

    // to be assigned to the new namespace

    return Constr;

}());

var arr = new MYAPP.utilities.Array(obj);

Importing Globals into a Module

In a common variation of the pattern, you can pass arguments to the immediate function that wraps the module. You can pass any values, but usually these are references to global variables and even the global object itself. Importing globals helps speed up the global symbol resolution inside the immediate function, because the imported variables become locals for the function.

MYAPP.utilities.module = (function (app, global) {

    // references to the global object

    // and to the global app namespace object

    // are now localized

}(MYAPP, this));

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 5.4 Module Pattern的更多相关文章

  1. JavaScript Patterns 5.5 Sandbox Pattern

    Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...

  2. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

  3. JavaScript Patterns 5.1 Namespace Pattern

    global namespace object // global object var MYAPP = {}; // constructors MYAPP.Parent = function() { ...

  4. JavaScript Patterns 4.2 Callback Pattern

    function writeCode(callback) { // do something... callback(); // ... } function introduceBugs() { // ...

  5. JavaScript Patterns 2.6 switch Pattern

    Principle • Aligning each case with switch(an exception to the curly braces indentation rule). • Ind ...

  6. Learning JavaScript Design Patterns The Module Pattern

    The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...

  7. JavaScript Module Pattern: In-Depth

    2010-03-12 JavaScript Module Pattern: In-Depth The module pattern is a common JavaScript coding patt ...

  8. Understanding the Module Pattern in JavaScript

    Understanding the Module Pattern in JavaScript Of all the design patterns you are likely to encounte ...

  9. JavaScript module pattern精髓

    JavaScript module pattern精髓 avaScript module pattern是一种常见的javascript编码模式.这种模式本身很好理解,但是有很多高级用法还没有得到大家 ...

随机推荐

  1. 介绍开源的.net通信框架NetworkComms框架 源码分析(七)ProtobufSerializer

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  2. 删除Android包

    Android删除包有很多种方法,其中一种通过Intent删除,代码如下: public boolean unload (String n){ boolean res = true; try{ // ...

  3. WebView的使用及添加进度条

    实现的效果比较简单类似于微信打开网页,头部有个进度条显示加载进度 下载地址:http://download.csdn.net/detail/qq_29774291/9666941 1.在安卓端加载一个 ...

  4. linux下c程序的链接、装载和库(1)

    读完<程序员的自我修养--链接.装载和库>相关章节,想来总结一下,若有错误,请指正,多谢. 1. 什么叫目标文件? 你的工程里有很多xxx.c这样的源文件,这些文件是文本文件,只有人能够认 ...

  5. Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境

    项目建设完成之后的结构: 数据库的表结构如下: 环境建设:搭建Maven环境.Tomcat环境.需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备): 开始创建工程: 1.创建 ...

  6. 【Effective Java】7、优先考虑泛型方法

    package cn.xf.cp.ch02.item27; import java.util.HashSet; import java.util.Set; public class Union { / ...

  7. 自定义开关ToggleButton的使用

    [代码]: toggleMe.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override p ...

  8. [翻译] Autofac 控制范围和生命周期

    原文链接:http://docs.autofac.org/en/latest/lifetime/index.html Lifetime 是指服务的实例在程序中存活多久 – 从最初的实例化到清理(dis ...

  9. SharePoint2013的头像显示和读取

      前言 有个时候SP的二次开发,需要用代码获取头像显示,如SP基于AD验证,AD有头像属性,做为头像数据来源(因为Exchange和lync的头像也来自AD),说道这里大家都知道有2种办法,一种从A ...

  10. 给SHP文件定义投影

    #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib.request import os def get_epsg_code(epsg ...