【发布时间】:2017-03-10 17:50:45
【问题描述】:
Node 的 module.exports 和 ES6 的 export default 有什么区别?我试图弄清楚为什么在 Node.js 6.2.2 中尝试 export default 时出现“__ 不是构造函数”错误。
什么有效
'use strict'
class SlimShady {
constructor(options) {
this._options = options
}
sayName() {
return 'My name is Slim Shady.'
}
}
// This works
module.exports = SlimShady
什么不起作用
'use strict'
class SlimShady {
constructor(options) {
this._options = options
}
sayName() {
return 'My name is Slim Shady.'
}
}
// This will cause the "SlimShady is not a constructor" error
// if in another file I try `let marshall = new SlimShady()`
export default SlimShady
【问题讨论】:
-
点赞,因为发现一个黑幕粉丝感觉真好
-
也为问题和马歇尔投了赞成票!
标签: node.js module ecmascript-6