exports和module.exports

共性

做用都是将文件模块的方法属性暴露给require返回的对象进行调用。数组

区别

包含关系bash

exports module.exportsui

也就是说module.exports能够替代exports,反之则不行。spa

实际上,exports是指向module.exports的引用,module.exports的初始值是一个空对象{},require()返回的是module.exports而不是exports.因此一旦module.exports有了新的引用,exports就和module.exports失去联系,导不出了。code

//person.js
exports.name = 'sicily';
console.log(module.exports)//{name:'sicily'}
module.exports = {name:'becky'};
复制代码
show.js
var person = require('./person');
console.log(person.name)//becky
复制代码

也能够这样理解:exports是给module.exports添加属性和方法。对象

返回数据类型ci

exports返回object对象,module.exports方法还能够单独返回一个数据类型。当须要返回一个类,数组,字符创时,就必须用module.exports.string

相关文章
相关标签/搜索