We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
初学者可能对NodeJs中的Module的导出傻傻分不清楚,我们来进行解惑。
这组概念是 CommonJs 的标准。
CommonJs
最终模块生效的是 module.exports ,而 exports 只是 module.exports 的一个默认简写引用而已。
module.exports
exports
它们的关系是:module.exports = exports => [内存堆栈]。
上面的表达式隐含了以下含义:
举些栗子:
//A.js exports.name = 'zhangsan'; exports.getId = function(){ return 1; }; //B.js let a = require('A.js'); console.log(a.name);//zhangsan
//A.js exports.name = 'zhangsan'; module.exports.getId = function(){ return 1; }; //B.js let a = require('A.js'); console.log(a.name);//error
因为在 A.js 中,module.exports 不等于 exports,二者已指向不同的内容。
A.js
这组概念是 ES6 的语法。
ES6
作用同 CommonJs 的规范一样,都是模块化导出模块的作用。
二者的关系是: export default 是 export 的一种特例。
export default
export
import {xx,yy} from 'zzz.js'
import zzz from 'zzz.js'
其实,最主要的作用就相当于是给导出的模块有个默认的值一样。
其实在Node.js中,这2组概念是可以混合着用的,但是为了保证概念的统一性,建议还是结对使用。
建议:
export + import
exports + require
(全文完)
The text was updated successfully, but these errors were encountered:
sunmaobin
No branches or pull requests
初学者可能对NodeJs中的Module的导出傻傻分不清楚,我们来进行解惑。
exports/module.exports
这组概念是
CommonJs
的标准。最终模块生效的是
module.exports
,而exports
只是module.exports
的一个默认简写引用而已。它们的关系是:
module.exports
=exports
=> [内存堆栈]。上面的表达式隐含了以下含义:
module.exports
=exports
,二者指向同样的内存指向;exports
,那么module.exports
也将同样得到修改;module.exports
(指向新的内存堆栈),exports
并不会得到修改,还是旧的内容,这时候二者将不相同。举些栗子:
exports
简写,最终的结果就相当于module.exports
的结果。exports
也有module.exports
,那么最终以module.exports
结果为主。因为在
A.js
中,module.exports
不等于exports
,二者已指向不同的内容。export/export default
这组概念是
ES6
的语法。作用同
CommonJs
的规范一样,都是模块化导出模块的作用。二者的关系是:
export default
是export
的一种特例。export
可以使用多次(导出多个元素),但是export default
只能有一个。export default
,则必须这么写:import {xx,yy} from 'zzz.js'
;如果有,则可以这么写:import zzz from 'zzz.js'
。其实,最主要的作用就相当于是给导出的模块有个默认的值一样。
补充一点
其实在Node.js中,这2组概念是可以混合着用的,但是为了保证概念的统一性,建议还是结对使用。
建议:
export + import
exports + require
(全文完)
The text was updated successfully, but these errors were encountered: