内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

nodejsjs模塊加載-創(chuàng)新互聯(lián)

本文地址:http://www.cnblogs.com/jasonxuli/p/4381747.htmlnodejsjs模塊加載

nodejs的非核心模塊(core module)加載主要使用的就是module.js。

10余年的松原網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整松原建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“松原網(wǎng)站設(shè)計(jì)”,“松原網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

項(xiàng)目主模塊(index.js/main.js等)加載使用的應(yīng)該是module.js中的runMain(),其他js模塊加載流程基本上是:

1,獲取js文件信息;

2,new Module();

3,讀取js文件內(nèi)容,封裝到一個(gè)function中,同時(shí)注入module本身,module.exports,包裝過的require函數(shù)等變量;

4,在某個(gè)上下文環(huán)境中執(zhí)行這個(gè)封裝后的function;

5,返回module.exports;

下面是摘出的主要代碼

module.js:

// Loads a module at the given file path. Returns that module's
// `exports` property.Module.prototype.require = function(path) { assert(path,'missing path'); assert(util.isString(path),'path must be a string'); return Module._load(path, this); }; // Check the cache for the requested file. // 1. If a module already exists in the cache: return its exports object. // 2. If the module is native: call `NativeModule.require()` with the // filename and return the result. // 3. Otherwise, create a new module for the file and save it to the cache. // Then have it load the file contents before returning its exports // object.Module._load(filename, this){ If(fileNamein Module._cache) Return Module._cache[filename];
  var module = new Module(filename, parent);   Module._cache[filename]= module;   Module.load(filename);
  // 先把模塊放到緩存然后再去加載內(nèi)容,可以解決循環(huán)依賴的問題。
  // 參見https://nodejs.org/api/modules.html的Cycles部分。
  return module.exports; } Module.prototype.load(filename){ var extension = path.extname(filename) || ‘.js’;
Module._extensions[extension](this, filename){ var content = fs.readFileSync(filename, ‘utf-8’); module._compile(content, filename);   } } // Run the file contents in the correct scope or sandbox. Expose // the correct helper variables (require, module, exports) to // the file. // Returns exception, if any.Module.prototype._compile = function(content, filename) { function require(path) { return self.require(path);   }   require.main= process.mainModule;   // Enable support to add extra extension types  require.extensions = Module._extensions;   require.cache= Module._cache;   // TODO: 每個(gè)模塊加載到自己的context中會(huì)有什么不同?   // Set the environ variable NODE_MODULE_CONTEXTS=1 to make node load allmodules in their own context.   // Module._contextLoad = (+process.env['NODE_MODULE_CONTEXTS'] > 0);   // if (Module._contextLoad) { .... }  // create wrapper function  var wrapper = NativeModule.wrap(content);   // wrapper = '(function (exports, require, module, __filename, __dirname) { ' + content + ' });'  // runInThisContext()可以認(rèn)為是在某個(gè)上下文環(huán)境中執(zhí)行的eval()。   // compiledWrapper相當(dāng)于eval(‘(function(){....})’)的結(jié)果,也就是真正的Function?! ar compiledWrapper = runInThisContext(wrapper, { filename: filename });   // module.exports 和 exports 的區(qū)別:   // 這里只傳遞了exports的引用;而上面Module._load()返回的是module.exports,因此給module.exports賦值后,   // exports仍然指向之前的module.exports?! ar args = [self.exports, require, self, filename, dirname];   // 真正執(zhí)行compiledWrapper這個(gè)function,也就是執(zhí)行了filename.js的內(nèi)容。  return compiledWrapper.apply(self.exports, args); }

runInThisContext()就是vm.js里的runInThisContext(),vm.runInThisContext()調(diào)用了src/node_contextify.cc中的RunInThisContext()。

https://github.com/joyent/node/blob/master/lib/vm.js

https://github.com/joyent/node/blob/master/src/node_contextify.cc

 擴(kuò)展:

https://github.com/brianmcd/contextify

The main difference between Contextify and Node's vm methods is that Contextify allows asynchronous functions to continue executing in the Contextified object's context.

博客地址:http://www.cnblogs.com/jasonxuli/

分享名稱:nodejsjs模塊加載-創(chuàng)新互聯(lián)
鏈接分享:http://www.rwnh.cn/article8/dspgop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、企業(yè)建站、微信公眾號(hào)網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
赣州市| 祁阳县| 铁力市| 普兰县| 蒙阴县| 岳西县| 临湘市| 方城县| 多伦县| 沂水县| 咸丰县| 南郑县| 抚顺县| 康平县| 邯郸县| 沂源县| 宜宾市| 成武县| 西林县| 宕昌县| 阳泉市| 彰化市| 行唐县| 台中市| 大渡口区| 任丘市| 深州市| 长寿区| 南安市| 建阳市| 兴仁县| 长汀县| 康平县| 天祝| 广丰县| 吉水县| 长寿区| 黑水县| 天峨县| 阜南县| 林口县|