Fun with Stamps. Episode 22. JavaScript instanceof as composable stamp
Hello. I’m developer Vasyl Boroviak and welcome to the twenty second episode of Vasyl Boroviak presents Fun with Stamps.
TL;DR: This is always false myObj instanceof MyStamp === false
. But it will become true if you compose @stamp/instanceof
to MyStamp
.
Occasionally people ask:
How would I check if object is an instance of a particular stamp?
It is not recommended to use instanceof
in JavaScript in general. It’s dangerous. Especially with stamps. However, old habits die hard so we developed @stamp/instanceof
utility stamp.
The instanceof
always returns false
for any stamp and object out there.
let MyStamp = stampit({ ... }) // you have a stamp
let myObj = MyStamp() // create instance of your stampconsole.log(myObj instanceof MyStamp) // false
But after composing InstanceOf
utility stamp into your stamp the instanceof
works as if you were using classes.
const InstanceOf = require('@stamp/instanceof')
MyStamp = MyStamp.compose(InstanceOf) // compose it into your stamp
myObj = MyStamp() // create instance of your stampconsole.log(myObj instanceof MyStamp) // true
Behind the scenes
Here is the full source code of the @stamp/instanceof
utility stamp.
As you can see, we are using the well-known Symbol “hasInstance”. If you apply it to a function (stamps are functions) then JavaScript runtime will use your implementation of instanceof
instead of the default one.
const compose = require('@stamp/compose');
const stampSymbol = Symbol.for('stamp');
module.exports = compose({
methods: {},
composers: [({ stamp }) => {
// Attaching to object prototype to save memory
stamp.compose.methods[stampSymbol] = stamp;
Object.defineProperty(stamp, Symbol.hasInstance, {
value (obj) {
return obj && obj[stampSymbol] === stamp;
}
});
}]
});
We are using the composers feature here. Every time a composition happens we attach the “stamp” symbol to prototype (aka methods metadata). Then we attach our instanceof
implementation to the stamp. The implementation tries to retrieve the “stamp” symbol value from an object and compare it with the stamp.
Reminder. There is stampit_org twitter where we publish all the news.
Have fun with stamps!
The rest of the episodes:
- Episode 1. Stamp basics
- Episode 2. Dependency injection in FP
- Episode 3. Comparing with the ES2015 classes
- Episode 4. Implementing stamps yourself in 30 LOC
- Episode 5. Composition design pattern
- Episode 6. Statics — properties on stamps
- Episode 7. Early and late dependency injection
- Episode 8. Tracking and overriding composition
- Episode 9. Detaching compose()
- Episode 10. My stamp mental model
- Episode 11. Interfering composition
- Episode 12. New @stamp home
- Episode 13. Method collision control
- Episode 14. New @stamp/it as a replacement of Stampit
- Episode 15. The @stamp/ modules ecosystem
- Episode 16. TypeScript mix-in classes vs Stamps
- Episode 17. Easy 100% unit test coverage in JS
- Episode 18. Dependency injection paradise
- Episode 19. Java/C# abstract methods in JavaScript
- Episode 20. Stampit v4
- Episode 21. Private data in JavaScript. 4 ways using stamps
- Episode 22. JavaScript instanceof as composable stamp (this article)
- Episode 23. New stampit.js.org with all the docs
- Episode 24. New “name” feature