我遇到了一个非常奇怪的错误。我正在尝试调试为什么我无法获取此模块 es6-error使用 babel 6。测试总是失败。这些测试之一相当简单,自定义错误的实例应该是一个错误。这两者都应该是true
。
console.log(err instanceof Error)
console.log(err instanceof ExtendableError)
下面是 jsbin 上完全相同的 src
代码的示例,其中两个日志都返回 true
。
如果您将该代码放入一个节点文件中并运行它,则会发生相同的行为。
很明显,测试失败的原因是模块导入无法正常工作,这奇怪地失败了。
import ExtendableError from './index'
var b = new ExtendableError();
console.log(b instanceof ExtendableError) // false
console.log(b instanceof Error) // true
为什么当 ExtendableError
的声明与 console.log
位于同一文件中时,我得到 true
, true
有效行为,当我在它自己的文件中有 ExtendableError
时,它会变成 false
,true
?
最佳答案
前往 babel slack channel ,并立即了解了这个插件,该插件允许向后兼容 babel 5。
https://github.com/loganfsmyth/babel-plugin-transform-builtin-extend
{
["babel-plugin-transform-builtin-extend", {
"globals": ["Error"],
"approximate": true
}]
}
关于javascript - 扩展错误 es6 类模块问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014123/