本文實例講述了nodeJS模塊簡單用法。分享給大家供大家參考,具體如下:
1.定義Student模塊,Teacher模塊
function add(student){ console.log('Add Student:'+student);}exports.add=add;function add(teacher){ console.log('Add Teacher:'+teacher);}exports.add=add;
2.定義kclass模塊
//引入student模塊var student=require('./student');//引入teacher模塊var teacher=require('./teacher.js');function add(teacherName,students){ teacher.add(teacherName); students.forEach(function(item,index){ student.add(item); });}//向模塊對象,注冊方法exports.add=add;
3.定義啟動模塊 index.js
var class1=require('./kclass.js');class1.add('高一班',['張三','李四']);
運行結果:
希望本文所述對大家nodejs程序設計有所幫助。
新聞熱點
疑難解答