Node.js中的模块接口module.exports

来源:互联网 发布:知乎 哈布斯堡 编辑:程序博客网 时间:2024/04/30 07:53

在写node.js代码时,我们经常需要自己写模块(module)。同时还需要在模块最后写好模块接口,声明这个模块对外暴露什么内容。

实际上,node.js的模块接口有多种不同写法。

返回一个JSON Object

如下代码是一个简单的示例。

 var exp = {    "version": "1.0.0",    "function1": null,    "module1": null, }; module.exports = exp;

这种方式可以用于返回一些全局共享的常量或者变量

举个栗子:

// MATH.jsvar MATH = {   "pi": 3.14,   "e": 2.72,};module.exports = MATH;

调用方式为

var MATH = require("./MATH")console.log(MATH.pi);

一次require多个模块

这种方式还可以用于返回几个require的其他模块,可以实现一次require多个模块

// module_collection.jsvar module_collection = {   "module1": require("./module1"),   "module2": require("./module2"),};module.exports = module_collection;

调用方式为

var module_collection = require("./module_collection");var module1 = module_collection.module1;var module2 = module_collection.module2;// Do something with module1 and module2

返回函数

其实这种方式还有个变种,如下,通常可以返回几个函数

 // functions.js var func1 = function() {    console.log("func1"); }; var func2 = function() {   console.log("func2"); };exports.function1 = func1;exports.function2 = func2;

调用方式为

var functions = require("./functions");functions.function1();functions.function2();

返回一个类

返回一个构造函数,也就是一个类

如下是一个简单的示例。

// CLASS.jsvar CLASS = function(args) {  this.args = args;}CLASS.prototype.func = function() {  console.log("CLASS.func");  console.log(this.args);};module.exports = CLASS;

调用方法为

var CLASS = require("./CLASS")var c = new CLASS("arguments");

返回一个普通函数

如下是一个简单的示例

// func.jsvar func = function() {  console.log("this is a testing function");};module.exports = func;

调用方法为

var func = require("./func");func();

返回一个对象object

如下是一个简单的示例

// CLASS.jsvar CLASS = function() {  this.say_hello = "hello";};CLASS.prototype.func = function() {  console.log("I say " + this.say_hello);};module.exports = new CLASS();

调用方法为

var obj = require("./CLASS");obj.func();

返回一个单例 singleton

有时候我们需要模块返回一个单例 singleton.

也就是如下两种形式

// MATH.jsvar MATH = {  "pi": 3.14,  "e": 2.72,};module.exports = MATH;

以及

// CLASS.jsvar CLASS = function() {  this.say_hello = "hello";};CLASS.prototype.func = function() {  console.log("I say " + this.say_hello);};module.exports = new CLASS();
0 0
原创粉丝点击