nodejs入门教程三:调用内部和外部方法示例

本文实例讲述了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程序设计有所帮助。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wypjgj.html