CommonJS module & module wrapper & module caching

three types of modules: local/built-in/third-party

in nodejs, each file is a module, but is isolated by default

how to ask nodejs not only execute index.js but also other files? This is where the commonjs module format comes into the picture.

a standard: how a module should be structured and shared
commonjs standard

./? dot slash refers to the same folder

require function loads the add module into index.js

local module uses require and module.exports

load module using the require function

by requiring a module, we are basically asking v8 to execute the code in that module

to reuse a block of code is to expose certain functionality that can be consumed by external files or apps. we can to that using the special module.exports object (available in nodejs)

the value of this?module.exports object is what the require function returns for the corresponding module, this allows us to store it in a constant

module.exports = add

const addFn = require("./add")

this is called a default export where you can refer to the returned value using any name

commonjs module format for exporting and importing functionality that nodejs adheres to

exports/require
require

each module in nodejs has its own scope, the way nodejs achieve that is with the immediately invoked function expression (IIFE) in JS, each function gets its own private scope

(function(){})()

wrap it with parentheses to convert it into a function expression

add parentheses at the end to immediately invoke it

under the hood nodejs uses this?IIFE pattern

IIFE?pattern with function wrapper
module scope

module wrapper
5 parameters: exports, require, module, __filename, __dirname

by wrapping each module code in this IIFE with 5 parameters, nodejs provides few global looking variables that are actually specific to the module, this is how we get access to require and module.exports within a file, they are injected during execution by nodejs

local variables in debug panel: convenience variables

__dirname: directory name or folder name of current module

__filename: file name of current module

exports

require: import a module by path

module: reference to the current module

this

__dirname

//module wrapper function

(function(exports,require,module,__filename,__dirname){

????const obj = require('./')

????module.exports = log;??

????//or module.exports.log = log;

})

每個(gè)module都是一個(gè)對(duì)象
module wrapper function

module caching

in Nodejs when we require a new module, it is loaded and cached for subsequent loading

creating a new instance of superHero
newSuperHero name is also Superman

in line 1, the superhero module is loaded and cached (remenbered), so next time we require the same module on line 9, nodejs will remember has already been loaded before, reuse that instead of doing the additional work of loading it brand new, in this case it is going to load the same object as line one. Since objects are passed by reference, we get the same object whose name has been modified to Superman. This is how module caching works in nodejs, in real world application, the same module is typically imported in several different files and caching is what helps with performance.

require.cache

on line 6, execution does not bring us back to the superHero module, it goes straight to the next line. The code inside nodeJS is not parsed again, the same object is returned.

How do we deal with scenarios where we need to create separate instances? Instead of exporting an instance, we export the class itself.

Instead of exporting an instance, export the class itself.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容