nodejs入门教程三:调用内部和外部方法示例
本文实例讲述了nodejs入门教程之调用内部和外部方法。分享给大家供大家参考,具体如下:
1.创建fun.js
varfun3=require('./fun3');
varfun2=require('./fun2');
functionfun1(){
console.log("我是fun1");
//exports的方式:exports是给module.exports添加属性和方法
//fun2.obj.add(1,2);
//fun3.print();
//module.exports的方式
fun2.add(1,2);
fun3();
}
fun1();
2.创建fun2.js
varobj={
reduce:function(a,b){
returna-b;
},
add:function(a,b){
console.log("我是fun2的add方法:");
console.log(a+b);
}
}
//exports.obj=obj;
module.exports=obj;
3.创建fun3.js
functionprint(){
console.log("我是fun3的方法");
}
//exports.print=print;
module.exports=print;
4.执行fun.js
结果:
我是fun1 我是fun2的add方法: 3 我是fun3的方法
希望本文所述对大家nodejs程序设计有所帮助。