[Node.js] Exporting Modules in Node
In this lesson, you will learn the difference between the exports statement and module.exports. Two examples are demonstrated, each accomplishing the same task but one using export statements and one using module.exports. You will also learn the basic thumb rule to identify which is appropriate for your current needs.
// circle.js using the exports statement
var PI = Math.PI; exports.area = function(r){
return PI * r * r;
} exports.circumference = function(r){
return 2 * Pi * r;
}
// accessing the exported functions in the node shell
var circle = require('./circle.js'); circle.area(4);
circle.circumference(4);
---------------------
// using module.exports to demonstrate the same functionality
var PI = Math.PI; module.exports = function(r){
return {
area: function(){
return PI * r * r;
},
circumference: function(){
return 2 * PI * r;
}
}
}
// accessing the exposed functions in the node shell
var circle = require('./circle.js'); var myCircle = circle(4); myCircle.area();
myCircle.circumference();
To summarize that, the general thumb rule is use the exports statement to export instances of modules. Use the module.exports statement to export JavaScript objects.
[Node.js] Exporting Modules in Node的更多相关文章
- Node.js & ES modules & .mjs
Node.js & ES modules & .mjs Node.js v13.9.0 https://nodejs.org/api/esm.html https://nodejs.o ...
- node --experimental-modules & node.js ES Modules
node --experimental-modules & node.js ES Modules how to run esm modules in node.js cli $ node -v ...
- Node.js & ES Modules & Jest
Node.js & ES Modules & Jest CJS & ESM CommonJS https://en.wikipedia.org/wiki/CommonJS ht ...
- [Node.js] 05 - Modules and Function
一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码.JSON 或者编译过的C/C++ 扩展. 模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的. No ...
- node.js系列笔记之node.js初识《一》
node.js系列笔记之node.js初识<一> 一:环境说明 1.1 Linux系统CentOS 5.8 1.2 nodejs v0.10.15 1.3 nodejs源码下载地址 htt ...
- node.js入门系列(一)--Node.js简介
什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需要独立运行的JS,NodeJS就是一个解析器. 每一种解析器都是一 ...
- Node.js的安装以及Node.js的模块管理
索引: Node.js的安装以及Node.js的模块管理Node.js开发环境搭建以及对ES6的支持Node.js构建Vue.js项目Vue.js单文件组件的开发基于Vue.js的UI组件(Eleme ...
- Installing Node.js via package manager | Node.js
Installing Node.js via package manager | Node.js i386 (32-bit)
- Node.js入门教程:Node.js如何安装配置并部署第一个网站
前言:作为一个资深的前端开发人员,不懂的Node.js 那你绝对是不能跟别人说你是资深的前端程序猿滴! 今天洋哥就来和大家一起学习被大牛称之为前端必学的技能之一Node! 那么Node到底是什么呢? ...
随机推荐
- Razor模板引擎
Razor模板引擎 阅读目录 一.简介 二.非Mvc中使用Razor 三.总结 回到目录 一.简介 在MVC以外的场景中,我们往往需要完成一些模板引擎生成代码或页面的工作:在以前我们一般常用的有Raz ...
- AndroidStudio Gradle版本不匹配问题
报错信息: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } Error:(1, 1) A problem occurr ...
- Word2003中如何使封面和目录中不插入页码
Word2003中如何使封面和目录中不插入页码?? 转载自: http://blog.zzedu.net.cn/user1/zhaoweijie/archives/2010/187266.html ...
- hbase rowkey设计的注意事项
充分利用有序性 1.1 如果要scan操作,且不是很频繁,可以利用rowkey的有序性将需要一起扫描的数据放到一起.例如直接用时间戳.这样就可以按时间scan了.这个只要是简单的全表扫描都行. 1.2 ...
- 把内表 itab1 的 n1 到 n2 行内容附加到 itab2 内表中去.
语法:append lines of itab1 [ from n1 ] [ to n2 ] to itab2. DATA:BEGIN OF gt_00 OCCURS 0, l_01 ...
- SAP修改前台屏幕字段文本
首先,要找到需要修改文本所对应的数据元素: 其次,进入CMOD,点击菜单栏中“转到--文本增强--关键字--更改”,填入数据元素,进入下一屏幕.将显示的文本全部改为自己需要的文本,保存即可.
- 【技术贴】Eclipse 右键打开当前文件所在文件夹
1.使用插件,百度:OpenExplorer_1.5.0.v201108051513.jar 2.默认情况下使用eclipse打开当前文件所在文件夹很麻烦,需要右键点击 Package Explore ...
- Mysql大表查询优化技巧总结及案例分析
http://www.169it.com/article/3219955334.html sql语句使用基本原则:1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 orde ...
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage Problem Description The stable marriage problem consists of matching members o ...
- 裸眼3D立体显示技术原理详解
众所周知,现实世界是一个三维空间,除去时间这一维度,现实世界是由长度.宽度和高度三个维度组成,我们每天就生活在这个三维世界中,而现有的显示设备大多数都只能显示二维信息,并不能带给人真实的三维感觉.为了 ...