Javascript 捕获某种程度上影响对象评估的错误

Javascript 捕获某种程度上影响对象评估的错误,javascript,error-handling,try-catch,Javascript,Error Handling,Try Catch,我的代码结构如下: class CustomError { constructor(status, error = null, message = null) { this.status = status; this.error = error; this.message = message; } } const foo = async function() { throw new CustomError('foo error'); } const bar

我的代码结构如下:

class CustomError {
  constructor(status, error = null, message = null) {
    this.status = status;
    this.error = error;
    this.message = message;
  }
}

const foo = async function() {
  throw new CustomError('foo error');
}

const bar = async function() {
  try {
    foo();
  } catch (error) {
    if (error instanceof CustomError) {
      throw error;
    }
    throw new CustomError('bar error');
  }
}

bar();
这会抛出条错误

我把它放在
if
语句之前:
console.log(CustomError的错误实例,error)
哪些日志:

false
CustomError {status: "foo", message: null, error: null}

这似乎是矛盾的。如果它将
error
记录为
CustomError
不应该
error instanceof CustomError
返回
true

这是否回答了您的问题?这回答了你的问题吗?