3 ms·
Now you can create an API where it’s not enough to pass in an object that has all the properties the API requires, it has to have some special inscrutable magic
by jwalton 6y ago
Now you can create an API where it’s not enough to pass in an object that has all the properties the API requires, it has to have some special inscrutable magic that you can’t programmatically observe. I can only imagine the sort of confusion and horror an API that relies on this would generate in novice developers.
- notriddle 6y agoYou can polyfill this using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... -- you "brand" your objects by stashing references to them in WeakMaps, and hide the WeakMap using a closure.
- kjeetgill 6y agoOnly sorta! From the article: > Since private names are only available inside the class which defines them, the test must also occur inside the class So code in other classes can't introduce dependance on this atleast. It's not any pickier than checking the class of an object, but only within the class itself. It's a narrower footgun that I'd first considered it to be.