Why does this result in false?
'use strict';
class InvalidCredentialsError extends Error {
constructor(msg) {
super(msg);
this.name = 'InvalidCredentialsError';
}
}
const err = new InvalidCredentialsError('');
console.log(err instanceof InvalidCredentialsError);
But this returns true:
console.log(err instanceof Error);
This works...you need to construct an instance of your type, then instanceof
will return whether your object is indeed an instance of that type.
'use strict';
class InvalidCredentialsError extends Error {
constructor(msg) {
super(msg);
this.name = 'InvalidCredentialsError';
}
}
var err = new InvalidCredentialsError("Hello, world!");
console.log(err instanceof InvalidCredentialsError); // true
Note const errClass = InvalidCredentialsError;
will just create an alias for your type, so you could do this...
var err = new errClass("Hello, alias");
console.log(err instanceof errClass); // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With