Last active
February 23, 2018 23:09
-
-
Save abstractmachines/18ea0dc6b8b98e307e937806b772f974 to your computer and use it in GitHub Desktop.
Well Known Symbols : instanceof via custom Symbol.hasinstance
This file contains hidden or 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
| /* Well Known Symbols : Symbol.hasinstance | |
| sources: | |
| 1 - https://www.keithcirkel.co.uk/metaprogramming-in-es6-symbols/ | |
| 2 - http://exploringjs.com/es6/ch_symbols.html | |
| 3 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof | |
| 4 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/hasInstance | |
| Well Known Symbols are Reflection via implementation in ES6. | |
| Reflection in Metaprogramming: observing low level internals of a programming language/program. | |
| One example of Well Known Symbols in JS is Symbol.hasInstance, which instanceof uses under the hood. | |
| */ | |
| // using instanceof : | |
| function ClassyFunc() {} | |
| let c = new ClassyFunc(); | |
| if (c instanceof ClassyFunc) { | |
| console.log('instanceof.') | |
| } | |
| else { | |
| console.log('not instanceof.') | |
| } | |
| // > instanceof | |
| /* Let's implement that by hand. | |
| Under the hood, instanceof uses (rhs)[Symbol.hasInstance](lhs), | |
| where rhs is right hand side and lhs is left hand side. | |
| Symbol is static scoped. | |
| */ | |
| class ArrayClass { | |
| static [Symbol.hasInstance](input) { | |
| return Array.isArray(input) | |
| } | |
| } | |
| let a = [] | |
| let b = 3 | |
| console.log(a instanceof ArrayClass) | |
| // > true | |
| console.log(a instanceof ArrayClass) | |
| // > false |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instanceof lies. Typing matters
https://medium.com/@_ericelliott/javascript-does-not-have-static-types-69492ce923b5