国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答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
主站蜘蛛池模板: 国产99久久九九精品免费 | 国产网站免费在线观看 | 久久亚洲精品国产精品777777 | 亚欧精品一区二区三区 | 99久久精品国产国产毛片 | 精品国产亚一区二区三区 | 天堂亚洲欧美日韩一区二区 | 久久精品国产欧美日韩99热 | 欧美瑟图| 免费一区二区视频 | 精品123区| 日韩精品一二三区 | 伊人久久精品一区二区三区 | 欧美一区二区三区日韩免费播 | 中文字幕美日韩在线高清 | 另类专区 亚洲 | 欧美日韩影院 | 一级免费a | 欧美成人猛男性色生活 | 欧美日韩成人 | 二区三区不卡不卡视频 | a男人的天堂久久a毛片 | 午夜黄色在线观看 | 91午夜精品亚洲一区二区三区 | 91大神在线精品视频一区 | 国产亚洲一欧美一区二区三区 | 亚洲一区二区三区在线播放 | 亚洲视频在线观看视频 | 亚洲欧洲国产成人综合一本 | 可以免费看的毛片 | 97热久久免费频精品99国产成人 | 久久99久久精品国产99热 | 国产一区二区在线视频 | 欧美日韩在线视频 | 久久伊人免费视频 | 欧美在线综合 | 国内精品久久久久激情影院 | 国产毛片一级国语版 | 中文字幕一区二区三区不卡 | 精品久久久一二三区 | 欧美第一网站 |