Created
June 1, 2016 05:24
-
-
Save ericelliott/f15875950310c6b51be31286454cfbbd to your computer and use it in GitHub Desktop.
instanceof doesn't mean what you think it means...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// instanceof is a prototype identity check. | |
// NOT a type check. | |
// That means it lies across execution contexts, | |
// when prototypes are dynamically reassigned, | |
// and when you throw confusing cases like this | |
// at it: | |
function foo() {} | |
const bar = { a: 'a'}; | |
foo.prototype = bar; | |
// Is bar an instance of foo? Nope! | |
console.log(bar instanceof foo); // false | |
// Ok... since bar is not an instance of foo, | |
// baz should definitely not be an instance of foo, right? | |
const baz = Object.create(bar); | |
// ...Wrong. | |
console.log(baz instanceof foo); // true. oops. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leads to things like: