1

I already read this post: Why does instanceof return false for some literals?

But didn't find any information about Error object work with instanceof operator. Here is my example:

class ErrorType1 extends Error {
  constructor(message) {
    super(message);
  }
}

class ErrorType2 extends Error {
  constructor(message) {
    super(message);
  }
}

function someOperation(data) {
  if (data === 1) {
    throw new ErrorType1('error 1');
  } else if (data === 2) {
    throw new ErrorType2('error 2');
  } else if (data === 0) {
    throw new Error('unknown error');
  }
}

function main(data) {
  try {
    someOperation(data)
  } catch(e) {
    console.log(e instanceof ErrorType2)
    if (e instanceof ErrorType1) {
      console.log(11)
      return e
    } else if (e instanceof ErrorType2) {
      console.log(22)
      return e
    }
    console.log(333)
    throw e;
  }
}

main(2)

I expected the e instanceof ErrorType2 expression should return a true value. But the stdout give me an false value and the console.log(333) statement executed. Here is the result:

enter image description here

Do I missing anything?

UPDATE

I used to guess it's my network issue, maybe some static resources don't load. But after I turn on the VPN, the result is same.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
  • This works as described for me. It outputs `true` followed by `22` which is exactly what I'd expect from your description. (I've executed that code in my browser console, not through the snippet, by the way). – Joachim Sauer Dec 17 '19 at 12:21
  • 2
    It looks to be an issue with the version of babel-standalone used by Stack Snippets. The code without Babel runs as expected, as does typing it directly into the [online transpiler](https://babeljs.io/repl) – CertainPerformance Dec 17 '19 at 12:23
  • @CertainPerformance OK, I was losing my mind here. I copied the snippet and played around with it and nothing about the behaviour made sense. I completely missed that Babel was ticked. – VLAZ Dec 17 '19 at 12:25
  • @CertainPerformance Yes, after I compile my example code, the compiled code works fine. So, is it a bug for SO? – Lin Du Dec 17 '19 at 12:29
  • @JoachimSauer Yes, run this example code in the console of chrome 73, it also works fine. – Lin Du Dec 17 '19 at 12:30
  • 1
    Babel [just cannot polyfill it](https://github.com/babel/babel/issues/4485) – Jonas Wilms Dec 17 '19 at 12:35

0 Answers0