标签:cpu 方法 end 的区别 log example red arguments 直接
node -v
查看是否装了node.js;const fs = require(‘fs‘) //加载内置的模块
fs.readFile(‘./a.md‘,‘utf-8‘,(err,text)=>{
if(err){
console.log(err)
return
}
text = text.toUpperCase()
fs.writeFile(‘./a-after.md‘,text,err=>{
if(err){
console.log(err)
return
}
console.log(‘写入完成‘)
})
console.log(text)
})
a.md :
hello world
//调用
const util = require(‘./util.js‘)
// let result = util.sum(1,2,3)
console.log(util.sum(1,2,3))
console.log(util.sum(2,3,4))
console.log(util.sum(3,4,5))
[util.js]
function sum(){
return [...arguments].reduce((v1,v2)=> v1 + v2)
}
module.exports.sum = sum
[example3.js]
const MarkdownIt = require(‘markdown-it‘)//Call modules
const fs = require(‘fs‘)//Call modules
md = new MarkdownIt()
fs.readFile(‘./a.md‘,‘utf-8‘,(err,text)=>{
if(err) throw err
let html = md.render(text)
fs.writeFile(‘./a.html‘,html,err=>{
if(err) throw err
console.log(‘conversion to complete‘)
})
})
[git bash]
//检查npm
npm --version
//安装模块
npm install markdown-it
[a.js]
let num = 1
const add = ()=> ++num
module.exports.num = num //导出
module.exports.add = add
[b.js]
let obj = require(‘./a.js‘)//实际得到的是module.exports对象
console.log(obj.num)//引用
console.log(obj.add())
console.log(obj.add())
[a.js]
//exports = module.exports 默认会有的
function sum(){
return [...arguments].reduce((v1,v2)=> v1+ v2)
}
//exports = sum 错误
//exports.sum = sum ok
module.exports = sum
[b.js]
const sum = require(‘./a.js‘)
console.log(sum(3,4))
console.log(sum(3,4,5))
标签:cpu 方法 end 的区别 log example red arguments 直接
原文地址:https://www.cnblogs.com/api9527/p/13326510.html