本文實例講述了nodejs入門教程之調用內部和外部方法。分享給大家供大家參考,具體如下:
1.創建fun.js
var fun3 = require('./fun3');var fun2 = require('./fun2');function fun1(){ 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
var obj = { reduce:function(a,b){ return a - b; }, add:function(a,b){ console.log("我是fun2的add方法:"); console.log(a+b); }}//exports.obj = obj;module.exports = obj;3.創建fun3.js
function print(){ console.log("我是fun3的方法");}//exports.print = print;module.exports = print;4.執行fun.js
結果:
我是fun1我是fun2的add方法:3我是fun3的方法
希望本文所述對大家nodejs程序設計有所幫助。
新聞熱點
疑難解答