报价单
class MyError extends Error {
  constructor(message) {
    super(message);
    this.message = message;
    this.name = 'MyError';
  }
}
  无需this.stack = (new Error()).stack;打招呼super()。
尽管以上代码无法输出堆栈跟踪,除非在Babel中调用this.stack = (new Error()).stack;或Error.captureStackTrace(this, this.constructor.name);。海事组织,这也许是一个问题。
实际上,堆栈跟踪可以在此代码段下Chrome console以及Node.js v4.2.1与此代码段一起输出。
class MyError extends Error{
        constructor(msg) {
                super(msg);
                this.message = msg;
                this.name = 'MyError';
        }
};
var myerr = new MyError("test");
console.log(myerr.stack);
console.log(myerr);
输出Chrome console。
MyError: test
    at MyError (<anonymous>:3:28)
    at <anonymous>:12:19
    at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
    at Object.InjectedScript.evaluate (<anonymous>:664:21)
输出 Node.js
MyError: test
    at MyError (/home/bsadmin/test/test.js:5:8)
    at Object.<anonymous> (/home/bsadmin/test/test.js:11:13)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:134:18)
    at node.js:961:3