node.js模块创建总结:
node.js模块的创建和使用流程本人刚刚接触node.js所遇到的一些小问题:我创建了5个js文件包括:student.js,teacher.js,class1.js,index.js
node.js模块的创建和使用流程

//student.js
function add(student){
	console.log('Add Student:' + student)
}
exports.add = add       //add方法暴露出去
//teacher.js
function add(teacher){
	console.log('Add Teacher:' + teacher)
}

exports.add = add     //add方法暴露出去
//class1.js
var student = require('./student')
var teacher = require('./teacher')
function add(teacherName, students){
	teacher.add(teacherName)
	students.forEach(function(item, index){
		student.add(item)  //学生在数组中的下标
	})
}
exports.add = add        //传统的模块实例,module.exports的辅助方法
//module.exports = add      //特别的对象类型,真实存在的
//index.js
var class1 = require('./class1')    //class是保留关键字,不能做变量名
class1.add('师傅', ['高富帅','白富美'])

刚开始使用模块由于自己的粗心,出现了一个小错误:
node.js模块的创建和使用流程错误代码:
node.js模块的创建和使用流程正确代码:
node.js模块的创建和使用流程Git Bash运行结果:
node.js模块的创建和使用流程最后,需要注意一点,class是保留关键字,不能做变量名。

相关文章:

  • 2021-12-22
  • 2022-12-23
  • 2021-11-06
  • 2021-06-25
  • 2021-10-02
  • 2021-11-15
猜你喜欢
  • 2022-12-23
  • 2022-02-07
  • 2021-11-09
  • 2021-04-06
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案