Inheriting from the Error object - where is the message property?

前端 未结 7 1235
面向向阳花
面向向阳花 2020-11-30 02:58

I noticed a strange behavior while defining custom error objects in Javascript:

function MyError(msg) {
    Error.call(this, msg);
    this.name = \"MyError\         


        
7条回答
  •  甜味超标
    2020-11-30 03:49

    In Node.js you can create a custom error like this:

    var util = require('util');
    
    function MyError(message) {
      this.message = message;
      Error.captureStackTrace(this, MyError);
    }
    
    util.inherits(MyError, Error);
    
    MyError.prototype.name = 'MyError';
    

    See captureStackTrace in node docs

提交回复
热议问题