const circle = require('./circle.js');
console.log(`The area of a circle of radius 4 is ${circle.area(4)}`);
const PI = Math.PI; exports.area = (r) => PI * r * r; exports.circumference = (r) => 2* PI * r;
const square = require('./square.js');
var mySquare = square(2);
console.log(`The area of my square is ${mySquare.area()}`);
module.exports = (width) => {
return {
area: () => width * width;
};
}
// 加载X模块 require(X) from module at path Y 1. If X is a core module. a. return the core module b. STOP 2. If X begins with './' or '/' or '../' a. LOAD_AS_FILE(Y + X) b. LOAD_AS_DIRECTORY(Y + X) 3. LOAD_NODE_MODULES(X, dirname(Y)) 4. THROW "not found" // 加载X文件 // 加载过程:X -> X.js -> X.json -> X.node LOAD_AS_FILE(X) 1. If [X] is a file, load [X] as JavaScript text. STOP 2. If [X.js] is a file, load [X.js] as JavaScript text. STOP 3. If [X.json] is a file, load [X.json] as JavaScript text. STOP 4. If [X.node] is a file, load [X.node] as JavaScript text. STOP // 加载入口文件 // 加载过程:X -> X/index.js -> X/index.json -> X/index.node LOAD_INDEX(X) 1. If [X/index.js] is a file, load [X/index.js] as JavaScript text. STOP 2. If [X/index.json] is a file, load [X/index.json] as JavaScript text. STOP 3. If [X/index.node] if a file, load [X/index.node] as JavaScript text. STOP // 加载文件夹 LOAD_AS_DIRECTORY(X) 1. If [X/package.json] is a file. a. Parse [X/package.json], and look for "main" field b. let M = X + (json main field) c. LOAD_AS_FILE(M) d. LOAD_INDEX(M) 2. LOAD_INDEX(X) // 加载node模块 LOAD_NODE_MODULES(X, START) 1. let DIRS = NODE_MODULES_PATHS(START) 2. for each DIR in DIRS; a. LOAD_AS_FILE(DIR/X) b. LOAD_AS_DIRECTORY(DIR/X) // 列出所有可能的node_modules路径 NODE_MODULES_PATHS(START) 1. let PARTS = path split(START); 2. let I = count of PARTS - 1 3. let DIRS = [] 4. while I > 0 a. If PARTS[I] = "node_modules" CONTINUE b. DIR = path join(PARTS[0 ... I] + "node_modules") c. DIRS = DIRS + DIR d. let I = I -1 5. return DIRS
console.log('a starting');
exports.done = false;
const b = require('./b.js');
console.log('in a, b.done = %j', b.done);
exports.done = true;
console.log('a done');
console.log('b starting');
exports.done = false;
const a = require('./a.js');
console.log('in b, a.done = %j', a.done);
exports.done = true;
console.log('b done');
console.log('main starting');
const a = require('./a.js');
const b = require('./b.js');
console.log('in main, a.done = %j, b.done = %j', a.done, b.done);
$ node app.js main starting a starting b starting in b, a.done = false b done in a, b.done = true in main, a.done =true, b.done = true
(function (exports, require, module, __filename, __dirname) {
// 你的模块代码
});
Object module {
id: String, // 模块标识,为该模块文件在系统中的绝对路径
exports: Object, // 该模块的导出对象
parent: Object | undefined, // 引用该模块的父模块
filename: String | null, // 最终解析的文件名称, 与__filename相同。
loaded: Boolean, // 该模块是否已经加载
children: Array, // 改模块的引用列表
paths: Array // 模块加载路径
}
Function require {
[Function], // 函数体
resolve: Function, // 根据模块标识解析模块,返回绝对路径
main: undefined | Object, // 应用的主(main)模块
extensions: {'.js':Function, '.json':Function, '.node':Function},
cache: Object // 模块缓存,以模块的绝对路径为key
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有