相关疑难解决方法(0)

为什么instanceof不在babel-node下的Error子类实例上工作?

我看到instanceof操作符在OS X上的babel-node版本6.1.18/Node版本5.1.0 Error下运行时不能处理子类的实例.为什么会这样?相同的代码在浏览器中运行良好,试试我的小提琴.

以下代码true在浏览器中输出,而在babel-node下则为false:

class Sub extends Error {
}

let s = new Sub()
console.log(`The variable 's' is an instance of Sub: ${s instanceof Sub}`)
Run Code Online (Sandbox Code Playgroud)

我只能想象这是由于babel-node中的一个bug,因为它instanceof适用于其他基类Error.

.babelrc

{
  "presets": ["es2015"]
}
Run Code Online (Sandbox Code Playgroud)

编译输出

这是babel 6.1.18编译的JavaScript:

'use strict';

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this …
Run Code Online (Sandbox Code Playgroud)

javascript node.js ecmascript-6 babeljs

73
推荐指数
2
解决办法
1万
查看次数

标签 统计

babeljs ×1

ecmascript-6 ×1

javascript ×1

node.js ×1