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. MVC之前的那点事儿系列(5):Http Pipeline详细分析(下)

    文章内容 接上面的章节,我们这篇要讲解的是Pipeline是执行的各种事件,我们知道,在自定义的HttpModule的Init方法里,我们可以添加自己的事件,比如如下代码: public class ...

  2. handlebars模板替换

    <html> <head> <meta charset="UTF-8" /> <!-- <script type="tex ...

  3. Scalaz(55)- scalaz-stream: fs2-基础介绍,fs2 stream transformation

    fs2是scalaz-stream的最新版本,沿用了scalaz-stream被动式(pull model)数据流原理但采用了全新的实现方法.fs2比较scalaz-stream而言具备了:更精简的基 ...

  4. 《Java4Android》视频学习笔记——抽象类和抽象函数

    抽象函数的语法特征 1.类是抽象的,对象是具体的.面向对象里面有个非常重要的特征:先抽象,后具体. 2.只有函数的定义,而没有函数体的函数叫做抽象函数. abstract void fun(); ( ...

  5. [PHP] 使用Socket提供Http服务

    我的SimpleLoader里面的一块 https://github.com/taoshihan1991/simpleloader <?php namespace Server; class S ...

  6. [小北De编程手记] : Lesson 04 玩转 xUnit.Net 之 Fixture(下)

    上一篇文章<[小北De编程手记] : Lesson 03 玩转 xUnit.Net 之 Fixture(上)>向大家介绍了xUnit.Net 共享数据的方式.Test Case的构造函数 ...

  7. Close与Dispose的区别

    Close与Dispose的区别: Close 是停业整顿,停业了,可以通过公关,再重开,物还是原来的物:只是关闭而已,没有释放真正的释放资源,可以重新打开:Close是关门Dispose是破产: D ...

  8. [Tool] 取得APP的Store URL Scheme (Android、iOS)

    [Tool] 取得APP的Store URL Scheme (Android.iOS) 前言 在企业网站中,如果希望使用URL连结的方式,开启Store APP来下载APP(非网页下载).开发人员可以 ...

  9. ENVI软件操作【数据显示操作——Overlay菜单操作】

    一.注记层(Annotation) 注记层是ENVI的一个数据类型,它的后缀名是.ann.往往作为栅格数据层,矢量数据层.三维场景会绘图图表的附加数据叠加在上面,还可以作为镶嵌图像时候的裁剪线.注记数 ...

  10. jQuery Countdown Timer 倒计时效果

    这个一款简单的 jQuery 倒计时插件,用于显示剩余的天数,小时,分钟和秒.倒计时功能是非常有用的一个小功能,可以告诉用户多久以后您的网站将会发布或者关闭进行维护,还可以用于举办活动的开始和停止的倒 ...