What is the difference between these two statements? They give different output (in google chrome console).
function Test() {
if (this instanceof Test) {
} else {
return new Test();
}
}
x = Test();
Test {}
function Test() {
if (!this instanceof Test) {
return new Test();
}
}
x = Test();
undefined
Mind = boggled
The issue is that the ! evaluates before the instanceof, so it's being treated as:
if ((!this) instanceof Test) { ... }
And, whether !this is true or false, neither value is an instanceof Test, preventing the new Test() from being returned.
Adding a grouping will force the desired order for "not an instance:"
if (!(this instanceof Test)) { ... }
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