Extending Error in Javascript with ES6 syntax & Babel

后端 未结 14 1860
你的背包
你的背包 2020-11-28 01:28

I am trying to extend Error with ES6 and Babel. It isn\'t working out.

class MyError extends Error {
  constructor(m) {
    super(m);
  }
}

var error = new          


        
14条回答
  •  感情败类
    2020-11-28 02:02

    I am trying to extend Error with ES6

    That class MyError extends Error {…} syntax is correct.

    Notice that transpilers still do have problems with inheriting from builtin objects. In your case,

    var err = super(m);
    Object.assign(this, err);
    

    seems to fix the problem.

提交回复
热议问题