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
?)
那么有ES6风格吗module.export =
?或者语法仍然保留在 ES6 中?或者在 ES6 中不再推荐该语法?
您可以使用
export default class A {
}
或
class A {
}
export default A;
导出为
exports["default"] = A;
module.exports = exports["default"];
在互操作部分有一个解释 here.
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"]
.
不支持 babel6 export default A
到 modules.export=A
你应该添加一个plugin
您可以在 Node v6 中使用以下内容:
"use strict"
class ClassName {
// class code
}
module.exports = ClassName
将以上文件保存为ClassName.js
将其导入另一个文件 Test.js:
"use strict"
var ClassName= require('./ClassName.js');
var obj = new ClassName( Vars . . . );
更多信息:
Here's an article on exporting classes from modules in Node v6
当我看到Babel编译后的代码时,它们似乎并不等价。
实际上,前者转化为exports.A = A
,后者不等同于module.exports = A
(也许是module.exports.A = A
?)
那么有ES6风格吗module.export =
?或者语法仍然保留在 ES6 中?或者在 ES6 中不再推荐该语法?
您可以使用
export default class A {
}
或
class A {
}
export default A;
导出为
exports["default"] = A;
module.exports = exports["default"];
在互操作部分有一个解释 here.
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"]
.
不支持 babel6 export default A
到 modules.export=A
你应该添加一个plugin
您可以在 Node v6 中使用以下内容:
"use strict"
class ClassName {
// class code
}
module.exports = ClassName
将以上文件保存为ClassName.js
将其导入另一个文件 Test.js:
"use strict"
var ClassName= require('./ClassName.js');
var obj = new ClassName( Vars . . . );
更多信息:
Here's an article on exporting classes from modules in Node v6