ES6`export class A`等同于`module.exports = A`? [英] Is ES6 `export class A` equivalent to `module.exports = A`?
问题描述
当我看到Babel编译的代码时,似乎并不等同。
实际上,前者转换为 exports.A = A
,这不等于 module.exports = A
(可能是 module.exports.A = A
?)
When I see the compiled code by Babel, they do not seem to be equivalent.
Actually, the former transforms to exports.A = A
, which is not equivalent to module.exports = A
(Maybe it is module.exports.A = A
?)
所以有一个ES6样式 module.export =
?还是语法仍然在ES6?或者ES6中不再推荐该语法?
So is there an ES6 style module.export =
? Or the syntax remains still in ES6? Or that syntax is no more recommended in ES6?
推荐答案
您可以使用
export default class A {
}
或
class A {
}
export default A;
哪些将导出为
exports["default"] = A;
module.exports = exports["default"];
有一个解释为什么在interop部分这里
There's an explanation why in the interop section here.
为了鼓励使用当导出默认导出,没有其他导出
module.exports
时,CommonJS和ES6模块将被设置为exports [default]
。
In order to encourage the use of CommonJS and ES6 modules, when exporting a default export with no other exports
module.exports
will be set in addition toexports["default"]
.
这篇关于ES6`export class A`等同于`module.exports = A`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!