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
- Node.js中的模块接口module.exports
- Node.js中的模块接口module.exports浅析
- Node.js中的模块接口module.exports浅析
- Node.js中的模块接口module.exports浅析
- Node.js模块导出exports 和 module.exports 的区别
- Node.js模块导出exports 和 module.exports 的区别
- Node.js模块导出exports 和 module.exports 的区别
- Node.js模块导出exports 和 module.exports 的区别
- Node.js中的module.exports和exports的区别
- node模块间传值(global、module.exports、exports)
- Node.js中require、exports、module模块的区别
- Node.js Module -moudule.exports vs exports
- Node.js中exports和module.exports
- Node.js的module.exports与exports
- node中的exports和module.exports
- node中的global、exports和module.exports
- Node.js的模块导出exports 和 module.exports 的区别
- node.js中的exports
- java计算混淆矩阵(分类指标:查准率P,查全率R,P和R的调和均值F1,正确率A)
- aidl的简单使用
- kubernetes源码阅读之controller-manager(三)
- 03-树1 树的同构 (25分)
- 检测linux网络IP和端口连通性
- Node.js中的模块接口module.exports
- Java Collection
- 每天一个Linux命令(21):find命令之xargs
- 64位网络字节序与主机字节序转换
- CentOS系列启动流程详解
- 基于Eclipse的arm-linux的远程GDB调试
- 每天一个Linux命令(22):find命令的参数详解
- QObject
- POJ 1061 青蛙(蛤蟆)的约会