导出模块成员

使用module.exports对象导出模块成员

每个模块内部都有一个 module 对象,代表当前模块,我们可以使用它的 exports 属性导出模块成员。该属性的初始值为一个空对象,我们只需要将被导出的成员添加为该对象的属性即可。例如:

 1 // 模块私有的成员
 2 function divide ( x, y ) {
 3     return x / y;
 4 }
 5 
 6 function multiply ( x, y ) {
 7     return x * y;
 8 }
 9 
10 // 如果我们想导出某个成员的话,只需要将它添加为 module.exports 对象的属性即可。
11 // 模块导出的成员
12 module.exports.add = function (x, y) {
13     return x + y;
14 };

module.exports.subtract = function (x, y) {

    return x - y;

};

 

使用module.exports的别名:exports对象

posted @ 2020-10-10 15:31  张尊娟  阅读(84)  评论(0编辑  收藏  举报