Node中Exports与module.export的使用与区别
最近在看《node开发实战详解》时有写疑问,所以自己就整理了一些资料。下面是node4.*的官方api文档(http://nodejs.cn/doc/node_4/modules.html#modules_module_exports),我有点看不懂,就拉出node.10*的官方api(https://nodejs.org/dist/v0.10.9/docs/api/modules.html#modules_module_exports)。
module.exports与exports的介绍
module.exports与exports都是将函数或者是方法暴露出去,require的时候进行调用,但是2者是有区别的。以下是代码:
//ex.js exports='danhuangmode'; //mex.js
module.exports='danhuangmode';
//call_ex_mex.js
var ex=require('./ex');
var mex=require('./mex');
console.log(ex);
console.log('\n');
console.log(mex);
执行结果:
引用exports提供方法,输出是为一个对象,引用module.exports提供方法,输出为字符串。
exports内部提供接口,在外部引用时之间的关系如何?
exports内部处理暴露方法,是如何处理的,看如下代码:
var string='this is in exports.js';function ex_fn () {console.log('this in funtion ex_fn'); }var exobj={str1:"str1 exobj",exobjfn: function () {console.log("in function");} };exports.string=string; exports.ex_fn=ex_fn; exports.exobj=exobj; exports=exobj;
调用代码:
var ex=require('./ex');console.log(ex.string); console.log(ex.ex_fn); console.log(ex.exobj); console.log(ex);
结果显示:
exports提供的所有接口,直接调用导出实例化的接口对象,会显示接口对象中所有提供的类型、函数、对象以及对象中的方法和对象。
module.exports对外提供接口如何处理?
//mex.js var ex_fn= function () {console.log('this in funtion ex_fn'); } module.exports=ex_fn;
调用代码mex.js:
//引用mex.js
var ex=require('./mex');
ex();
console.log(ex);
执行结果为:
直接将函数作为返回。
再看下面一个例子:
var person={name :"person's name",age :20,getAge: function () {return this.age;} }module.exports = person;
调用的代码:
var person=require('./modulex');console.log(person.name); console.log(person.age); console.log(person.getAge()); console.log(person);
显示的结果为:
返回为一个json对象,可以直接调用内部的函数、属性。
module.exports 与exports是什么关系?
module.exports = 'personname';exports.name=function () {console.log('this is person name'); }
调用 的脚本:
var person=require('./person');console.log(person); console.log(person.name);
执行结果:
personname
undefined
结果:
其实真正的接口是module.exports,exports是一个辅助工具。最终返回到是module.exports,而不是exports。
当module.exports没有任何属性和方法,exports将收集的所有信息都传递给module.exports,如果module.exports已经具有了属性和方法,exports所搜集的信息将会被忽略。
转载于:https://www.cnblogs.com/macoco/p/5398864.html
Node中Exports与module.export的使用与区别相关推荐
- Node.js中exports、module.exports、require之间的关系
Node中的js文件 Node中的每个JS文件都是一个单独的模块,模块中包含的核心变量:exports.module.exports.require nodejs中module文档 // 插入图片 E ...
- Node.js: exports 和 module.exports 的区别
我理解的exports 和 module.exports 的区别,欢迎大家吐槽~ 为了更好的理解 exports 和 module.exports 的关系,我们先来补点 js 基础.示例: app.j ...
- node.js中exports与module.exports的区别
CommonJS Node应用是由模块组成的,采用的是CommonJS模块规范. 根据规范一个文件就是一个模块,有自己单独的作用域,在一个文件中定义的变量,函数,类都是私有的,对其他的文件不可见. 根 ...
- node.js中exports与module.exports的区别分析
前言 关于Node.js中的exports和module.exports,很多时候都比较容易让人混淆,弄不清楚两者间的区别.那么我们就从头开始理清这两者之间的关系. 来源 在开发Node.js应用的时 ...
- Node.js 中 exports 和 module.exports 的区别
每一个模块中都有一个 module 对象, module 对象中有一个 exports 对象 我们可以把需要导出的成员都放到 module.exports 这个接口对象中,也就是 module.exp ...
- (node中)module.exports 和exports的区别
(node中使用)module.exports 和exports的区别是什么? 用一句话来说明就是,require只会去引用module.exports这个对象的导出,不会引用exports对象的,而 ...
- require 动态加载_require,exports,module.exports和import,export,export default
我们前端在开发过程中经常会遇到导入导出功能, 在导入时,有时候是require,有时候是import 在导出时,有时候是exports,module.exports,有时候是export,export ...
- nodeJS学习(9)--- nodeJS模块:exports vs module.exports
模块简介: 通过Node.js的官方API可以看到Node.js本身提供了很多核心模块 http://nodejs.org/api/ 这些核心模块被编译成二进制文件,可以 require('模块名') ...
- Node.js学习之(第二章:exports和module.exports)
前言 Node中,每个模块都有一个exports接口对象,我们需要把公共的方法或者字符串挂载在这个接口对象中,其他的模块才可以使用. Node.js中只有模块作用域,默认两个模块之间的变量,方法互不冲 ...
最新文章
- mysql+keepalived搭建高可用环境
- 多无人机对组网雷达的协同干扰问题 数学建模
- 飞桨PaddlePaddle送算力 | 每日登录即送12小时,连续5天还有加送!
- android 定位信息存哪,安卓手机便签系统存储位置在哪
- [Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning
- delphi中combobox键值对
- 【答案放在最后,看题看不到答案】2017年下半年软件设计师 上午选择题
- ACM MM 2021 北航鹏城实验室提出 SOD 新方法:互补三边解码器网络(CTDNet),兼顾高性能和高效率...
- HTML 5.1 的 14 个新特性(含使用案例)
- 我最大的乐趣是不厌其烦地收集人生的各种经历和体验。我喜欢享受人生的各种经历和体验所带给我的难以言表的乐趣...
- 力扣算法题—095不同的二叉搜索树【二叉树】
- input 上传图片_selnium远程机上传图片遇到的坑-布布扣
- 从零搭建美团饿了么外卖红包CPS小程序教程
- oracle查询优化不走缓存,Oracle彻底优化——优化内存
- 不一样的“人工智能语言”Prolog
- Python编程基础 第四章 编程练习 请定义一个Circle类,具体要求为:(1)每个Circle类对象可以存储圆心和半径信息;(2)具有设置圆心和半径的方法SetCenter和SetRadius
- LangChain:Prompt Templates介绍及应用
- 3D 电影、全息显示这些技术如何骗过我们的眼睛?
- latex中的希腊字母
- 苹果手机闹钟声音大小怎么调_苹果闹钟不响、声音小怎么办?