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. thinkphp中assign()和display()区别和用法

  2. 标签栏使用Demo二

    // //  PHTagViewFrame.m //  标签的使用二 // //  Created by 123 on 16/9/6. //  Copyright © 2016年 彭洪. All ri ...

  3. 新手编辑c语言的注意事项

    一般情况下新手都会犯的错误 1,注意在c语言中的大小写是有区别的,但在windows系统中默认没有差别,但是有时候也会出现bug. 2.在编程的时候注意定义你所使用的变量 3,每行代码的结尾注意要有分 ...

  4. mysql 更改默认数据目录

    http://www.cnblogs.com/chenny7/p/3642363.html 本文主要介绍在CentOS下通过yum命令安装MySQL之后,如何移动默认数据目录到指定位置. 安装mysq ...

  5. ActiveMQ消息队列介绍

    ActiveMQ是一个开源兼容Java Message  Service  (JMS) 1.1面向消息的中件间. 来自Apache Software Foundation. ActiveMQ提供松耦合 ...

  6. Atitit.事件机制 与 消息机制的联系与区别

    Atitit.事件机制 与 消息机制的联系与区别 1. 消息/事件机制是几乎所有开发语言都有的机制,在某些语言称之为消息(Event),有些地方称之为(Message).1 2. 发布/订阅模式1 3 ...

  7. pymysql 操作数据库

    一.简介 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同,但目前pymysql支持python3.x而后者不支持3.x版本 其执行语句与sql源码相似 二.使用 ...

  8. Could not publish to the server. java.lang.NullPointerException

    右键单击tomcat服务器,找到Properties,点下switch location就好了.

  9. 那些年,我们一起疯狂的C#

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  10. git常用命令表

    本文主要是用来记录一些在git管理的项目中常见的场景及其对应的命令,方便自己和他人使用的时候快速查询.如有不对,敬请指正. 查看某个git命令的帮助文档 git help [command] 查看各个 ...