本文實(shí)例講述了nodejs入門教程之調(diào)用內(nèi)部和外部方法。分享給大家供大家參考,具體如下:
1.創(chuàng)建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.創(chuàng)建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.創(chuàng)建fun3.js
function print(){ console.log("我是fun3的方法");}//exports.print = print;module.exports = print;4.執(zhí)行fun.js
結(jié)果:
我是fun1我是fun2的add方法:3我是fun3的方法
希望本文所述對(duì)大家nodejs程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注