<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        Node.js中對通用模塊的封裝方法_node.js

        來源:懂視網 責編:小采 時間:2020-11-27 21:24:02
        文檔

        Node.js中對通用模塊的封裝方法_node.js

        Node.js中對通用模塊的封裝方法_node.js:在Node.js中對模塊載入和執行進行了包裝,使得模塊文件中的變量在一個閉包中,不會污染全局變量,和他人沖突。 前端模塊通常是我們開發人員為了避免和他人沖突才把模塊代碼放置在一個閉包中。 如何封裝Node.js和前端通用的模塊,我們可以參考Undersco
        推薦度:
        導讀Node.js中對通用模塊的封裝方法_node.js:在Node.js中對模塊載入和執行進行了包裝,使得模塊文件中的變量在一個閉包中,不會污染全局變量,和他人沖突。 前端模塊通常是我們開發人員為了避免和他人沖突才把模塊代碼放置在一個閉包中。 如何封裝Node.js和前端通用的模塊,我們可以參考Undersco

        在Node.js中對模塊載入和執行進行了包裝,使得模塊文件中的變量在一個閉包中,不會污染全局變量,和他人沖突。

        前端模塊通常是我們開發人員為了避免和他人沖突才把模塊代碼放置在一個閉包中。

        如何封裝Node.js和前端通用的模塊,我們可以參考Underscore.js 實現,他就是一個Node.js和前端通用的功能函數模塊,查看代碼:

        代碼如下:
        // Create a safe reference to the Underscore object for use below.
        var _ = function(obj) {
        if (obj instanceof _) return obj;
        if (!(this instanceof _)) return new _(obj);
        this._wrapped = obj;
        };

        // Export the Underscore object for **Node.js**, with
        // backwards-compatibility for the old `require()` API. If we're in
        // the browser, add `_` as a global object via a string identifier,
        // for Closure Compiler "advanced" mode.
        if (typeof exports !== 'undefined') {
        if (typeof module !== 'undefined' && module.exports) {
        exports = module.exports = _;
        }
        exports._ = _;
        } else {
        root._ = _;
        }
        通過判斷exports是否存在來決定將局部變量 _ 賦值給exports,向后兼容舊的require() API,如果在瀏覽器中,通過一個字符串標識符“_”作為一個全局對象;完整的閉包如下:
        代碼如下:
        (function() {

        // Baseline setup
        // --------------

        // Establish the root object, `window` in the browser, or `exports` on the server.
        var root = this;

        // Create a safe reference to the Underscore object for use below.
        var _ = function(obj) {
        if (obj instanceof _) return obj;
        if (!(this instanceof _)) return new _(obj);
        this._wrapped = obj;
        };

        // Export the Underscore object for **Node.js**, with
        // backwards-compatibility for the old `require()` API. If we're in
        // the browser, add `_` as a global object via a string identifier,
        // for Closure Compiler "advanced" mode.
        if (typeof exports !== 'undefined') {
        if (typeof module !== 'undefined' && module.exports) {
        exports = module.exports = _;
        }
        exports._ = _;
        } else {
        root._ = _;
        }
        }).call(this);


        通過function定義構建了一個閉包,call(this)是將function在this對象下調用,以避免內部變量污染到全局作用域。瀏覽器中,this指向的是全局對象(window對象),將“_”變量賦在全局對象上“root._”,以供外部調用。

        和Underscore.js 類似的Lo-Dash,也是使用了類似的方案,只是兼容了AMD模塊載入的兼容:
        代碼如下:
        ;(function() {

        /** Used as a safe reference for `undefined` in pre ES5 environments */
        var undefined;
        /** Used to determine if values are of the language type Object */
        var objectTypes = {
        'boolean': false,
        'function': true,
        'object': true,
        'number': false,
        'string': false,
        'undefined': false
        };
        /** Used as a reference to the global object */
        var root = (objectTypes[typeof window] && window) || this;

        /** Detect free variable `exports` */
        var freeExports = objectTypes[typeof exports] && exports && !exports.nodeType && exports;

        /** Detect free variable `module` */
        var freeModule = objectTypes[typeof module] && module && !module.nodeType && module;

        /** Detect the popular CommonJS extension `module.exports` */
        var moduleExports = freeModule && freeModule.exports === freeExports && freeExports;

        /*--------------------------------------------------------------------------*/

        // expose Lo-Dash
        var _ = runInContext();

        // some AMD build optimizers, like r.js, check for condition patterns like the following:
        if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
        // Expose Lo-Dash to the global object even when an AMD loader is present in
        // case Lo-Dash was injected by a third-party script and not intended to be
        // loaded as a module. The global assignment can be reverted in the Lo-Dash
        // module by its `noConflict()` method.
        root._ = _;

        // define as an anonymous module so, through path mapping, it can be
        // referenced as the "underscore" module
        define(function() {
        return _;
        });
        }
        // check for `exports` after `define` in case a build optimizer adds an `exports` object
        else if (freeExports && freeModule) {
        // in Node.js or RingoJS
        if (moduleExports) {
        (freeModule.exports = _)._ = _;
        }
        // in Narwhal or Rhino -require
        else {
        freeExports._ = _;
        }
        }
        else {
        // in a browser or Rhino
        root._ = _;
        }
        }.call(this));
        再來看看Moment.js的封裝閉包主要代碼:
        代碼如下:
        (function (undefined) {
        var moment;
        // check for nodeJS
        var hasModule = (typeof module !== 'undefined' && module.exports);
        /************************************
        Exposing Moment
        ************************************/

        function makeGlobal(deprecate) {
        var warned = false, local_moment = moment;
        /*global ender:false */
        if (typeof ender !== 'undefined') {
        return;
        }
        // here, `this` means `window` in the browser, or `global` on the server
        // add `moment` as a global object via a string identifier,
        // for Closure Compiler "advanced" mode
        if (deprecate) {
        this.moment = function () {
        if (!warned && console && console.warn) {
        warned = true;
        console.warn(
        "Accessing Moment through the global scope is " +
        "deprecated, and will be removed in an upcoming " +
        "release.");
        }
        return local_moment.apply(null, arguments);
        };
        } else {
        this['moment'] = moment;
        }
        }

        // CommonJS module is defined
        if (hasModule) {
        module.exports = moment;
        makeGlobal(true);
        } else if (typeof define === "function" && define.amd) {
        define("moment", function (require, exports, module) {
        if (module.config().noGlobal !== true) {
        // If user provided noGlobal, he is aware of global
        makeGlobal(module.config().noGlobal === undefined);
        }

        return moment;
        });
        } else {
        makeGlobal();
        }
        }).call(this);
        從上面的幾個例子可以看出,在封裝Node.js和前端通用的模塊時,可以使用以下邏輯:
        代碼如下:
        if (typeof exports !== "undefined") {
        exports.** = **;
        } else {
        this.** = **;
        }
        即,如果exports對象存在,則將局部變量裝載在exports對象上,如果不存在,則裝載在全局對象上。如果加上ADM規范的兼容性,那么多加一句判斷:
        代碼如下:if (typeof define === "function" && define.amd){}

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Node.js中對通用模塊的封裝方法_node.js

        Node.js中對通用模塊的封裝方法_node.js:在Node.js中對模塊載入和執行進行了包裝,使得模塊文件中的變量在一個閉包中,不會污染全局變量,和他人沖突。 前端模塊通常是我們開發人員為了避免和他人沖突才把模塊代碼放置在一個閉包中。 如何封裝Node.js和前端通用的模塊,我們可以參考Undersco
        推薦度:
        標簽: js 模塊 node.js
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 18观看免费永久视频| 久久免费线看线看| 成人超污免费网站在线看| 亚洲黑人嫩小videos| 30岁的女人韩剧免费观看| 亚洲欧洲国产视频| 亚洲免费福利在线视频| 久久乐国产综合亚洲精品| 99精品全国免费观看视频| 亚洲日韩久久综合中文字幕| 成人毛片18女人毛片免费| 亚洲AV日韩AV永久无码色欲| 日本中文一区二区三区亚洲| 成人一级免费视频| 亚洲精品国偷自产在线| 日韩精品内射视频免费观看| 亚洲理论片在线观看| 成人毛片免费观看视频在线| 亚洲av无码专区在线电影| 亚洲七七久久精品中文国产| 免费萌白酱国产一区二区三区 | 亚洲熟女乱色一区二区三区| 成人毛片手机版免费看| 西西人体大胆免费视频| 亚洲va国产va天堂va久久| free哆啪啪免费永久| 久久久久久亚洲精品无码 | 亚洲AV永久青草无码精品| ww在线观视频免费观看| 亚洲高清乱码午夜电影网| 国产亚洲美女精品久久久2020 | 97精品免费视频| 亚洲精品无码专区| 亚洲婷婷五月综合狠狠爱| 18禁网站免费无遮挡无码中文| 亚洲午夜理论片在线观看| 国产亚洲精品资在线| 成人免费激情视频| 一级成人a免费视频| 国产成人亚洲合集青青草原精品| 亚洲精品高清在线|