目录
3.i5ting_toc可以将md文档转为html
模块化概念

利用require()方法加载模块,如果是用户自定义的模块,需要指定路径


- const custom=require('./01_模块作用域')
- console.log(custom);
- const username='张三'
- function sayHello(){
- console.log('大家好,我是'+username);
- }


module.exports():作用就是将模块内的成员共享出去,供外界使用
测试:模块暴露属性然后调用
- const username='zs'
- exports.username=username
- exports.age=20
- exports.sayHello=function(){
- console.log('hello');
- }
- const m=require('./04_exports对象')
-
- console.log(m);

2.注意:require()方法导入模块时,导入的结果,永远以moudle.exports为主
- /**
- 在一个自定义模块中,默认情况下:module.exports={}
- */
-
- const age=10
- module.exports.age=age
-
- // 向module.exports对象上挂在一个username属性
- module.exports.username='zs'
-
- // 向module.exports对象上挂在sayHello方法
- module.exports.sayHello=function(){
- console.log('hello');
- }
-
- // 让module.exports指向一个全新的对象
- module.exports={
- nickname:'小黑',
- sayHi(){
- console.log('hello');
- }
- }
-
- /**
- * 在外界使用require导入一个自定义模块的时候,
- * 得到的成员就是module.exports所指的对象
- */
- const m=require('./03_自定义模块')
-
- console.log(m);
moudle.exports和exports指向同一个对象,不过最终以exports指向的对象为主


3.总结:发现我们以exports的为主

exports和moudle.exports的使用误区
不建议在同一个模块同时使用exports和moudle.exports

CommonJS

npm与包


npm -v//查看版本
引入第三方的模块,然后require()进行调用,调用模块中的方法
- // 1.定义格式化时间的方法
- function dateFormat(dtStr){
- const dt=new Date(dtStr)
-
- const y=dt.getFullYear();
- const m=padZero(dt.getMonth()+1)
- const d=padZero(dt.getDate())
-
- const hh=padZero(dt.getHours())
- const mm=padZero(dt.getMinutes())
- const ss=padZero(dt.getSeconds())
-
- return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
-
- }
-
- // 定义补零操作
- function padZero(n){
- return n > 9 ? n : '0' + n
- }
-
- module.exports={
- dateFormat
- }
- //导入自定义的格式化时间的模块
- const TIME= require('./01_dataFormat')
-
- // 调用方法,进行时间的格式化
- const dt=new Date()
-
- const newDT=TIME.dateFormat(dt)
- console.log(newDT);

require('包名')
- // 1. 导入需要的包
- const moment=require('moment')
-
- /**
- * moment():获取当前时间
- * format():格式化时间方法
- * 年:YYYY
- * 月:MM
- * 日:DD
- * 时:hh
- * 分:mm
- * 秒:ss
- * 一个h或m或s代表不补零
- */
- const dt=moment().format('YYYY-MM-DD hh:mm:ss')
- console.log(dt);

6.npm install的效果


8.包的管理配置文件
记录了与项目有关的配置信息

个人感觉很像maven,npm intall相当于导入依赖,然后npm init相当于创建pom.xml
npm init -y //快速创建package.json
注意事项:
记得不能包含空格,也不能使用中文


10.当项目中缺少moudles包,需要加载
卸载包:npm unistall

包的分类



4.包的规范

发布包——还原HTML
转义字符:
g:代表全局匹配 ,以下的意思是:只要满足这四个字符匹配就进行替换操作
定义两个方法后进行暴露module.export={}
- // 定义转义html字符的函数
- function htmlEscape(htmlStr){
- return htmlStr.replace(/<|>|<&|"/g,(match)=>{
- switch(match){
- case '<':
- return '<'
- case '>':
- return '>'
- case '"':
- return '"'
- case '&':
- return '&'
- }
- })
- }
-
- // 定义还原html字符串的函数
- function htmlUnEscape(str){
- return str.replace(/<|>|"|&/g,(match)=>{
- switch(match){
- case '<':
- return '<'
- case '>':
- return '>'
- case '"':
- return '"'
- case '&':
- return '&'
- }
- })
- }
-
- module.exports={
- htmlEscape,
- htmlUnEscape
- }
然后我们测试类中调用这个模块
引入我们的第三方包,然后调用其中的方法并且打印
- const itcc=require('../itcc-tools/index')
-
- // 测试格式化时间的功能
-
- const dtStr=itcc.dateFormat(new Date())
- console.log(dtStr);
-
-
- // 测试转义html方法
- const htmlStr='
这是h1标签123 
' - const str=itcc.htmlEscape(htmlStr)
- console.log(str);
-
-
- // 测试还原html的方法
- const str2 = itcc.htmlUnEscape(str)
- console.log(str2);

模块的加载机制

内置模块的加载机制
1.模块具有优先级,内置模块的优先级>第三方模块

2.另外,第三方模块要指定路径的,比如../或者./;如果是内置模块,那就直接定义名称就可以了

3.如果省略了文件扩展名,它是有个顺序的


第三方模块加载机制

目录作为模块

如果以目录作为require的加载标识符,首先会去寻找目录下的package.json,并寻找里面的main属性作为入口——>否则去index.js

