ES6`export class A`等同于`module.exports = A`? [英] Is ES6 `export class A` equivalent to `module.exports = A`?

查看:392
本文介绍了ES6`export class A`等同于`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 to exports["default"].

这篇关于ES6`export class A`等同于`module.exports = A`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆