我看到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.
{
"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)