zoukankan      html  css  js  c++  java
  • 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.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)

  • 相关阅读:
    2020年终将过去
    Opus从入门到精通(一):简介
    0907 RTCP FB
    ClickHouse源码笔记6:探究列式存储系统的排序
    C++雾中风景17:模板的非推断语境与std::type_identity
    ClickHouse源码笔记5:聚合函数的源码再梳理
    ClickHouse源码笔记4:FilterBlockInputStream, 探寻where,having的实现
    ClickHouse源码笔记3:函数调用的向量化实现
    Doris开发手记1:解决蛋疼的MySQL 8.0连接问题
    声音克隆_论文翻译:2019_Transfer Learning from Speaker Verification to Multispeaker Text-To-Speech Synthesis
  • 原文地址:https://www.cnblogs.com/haokaibo/p/Module-Pattern.html
Copyright © 2011-2022 走看看