[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到底是什么呢? ...
随机推荐
- leetcode其余题目
1.Largest Rectangle in Histogram http://discuss.leetcode.com/questions/259/largest-rectangle-in-hist ...
- 分享关于学习new BufferedWriter()方法时常遇到的一个无厘头的问题
今天在学习IO的过程中,关于处理流BufferedWriter的使用时,遇到了一个很犯二但是又会让初学者经常没有避免的问题,百度后才发现有人和我一样二,这还是对java基础掌握得不牢固的原因啊. 首先 ...
- CSAPP(深入理解计算机系统)读后感
9月到10月8号,包括国庆七天,大概每天5小时以上的时间,把Computer System: A Programmer Perspective 2rd version(深入理解计算机系统)的英文版啃完 ...
- JVM 学习笔记(一)
JVM ----Java Virtual Machine (熟称:JAVA虚拟机),JVM 在执行JAVA程序的过程中将内容划分为若干个区域,其有各自的用途和管理机制.如下图: 1. 程序计 ...
- NOIP 2011 提高组 计算系数
有二项式定理 `\left( a+b\right) ^{n}=\sum _{r=0}^{n}\left( \begin{matrix} n\\ r\end{matrix} \right) a^{n-r ...
- 如何备份MySql的数据库
如何备份MySql的数据库 应该说一般的数据库都有BackUp的需求, MySql备份数据库的方式很简单, 但是在网上说得云里雾里, 也应该是我对MySql数据库不熟悉. 目标(Target) : ...
- Android用户界面 UI组件--TextView及其子类(三) EditView以及各种Span文字样式讲解
EditView和TextView的用法差不多,只是文字可编辑 小技巧: 设置EditText隐藏键盘 setInputType(0); 设置EditText不被输入法遮盖 getWindow() ...
- Android用户界面 UI组件--TextView及其子类(一) TextView
1.TextView android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none /web/email/phone/map/a ...
- IPVS实现分析
IPVS实现分析 IPVS实现分析 根据LVS官方网站的介绍,LVS支持三种负载均衡模式:NAT,tunnel和direct routing(DR). NAT是通用模式,所有交互数据必须通过均衡器:后 ...
- Chapter 17 Replication
Chapter 17 Replication Table of Contents 17.1 Replication Configuration 17.2 Replication Implementat ...