I want to intercept Boolean coercion for objects in JavaScript: https://www.zachleat.com/web/boolean-coercion/
Also posted to Bluesky.
11 replies
@webreflection worth a mention in the post, for sure. I don’t have control over all of the contexts in which the code will be used though 😭
@zachleat you are passing around a `typeof "object"` that is not `null` though ... that will already pass through tons of screening before any comparison against `false` or literally any other `falsy` values ... `new Boolean` is problematic like that, reason not many developers use it ... in Python, that would be a solved problem though 🥲
@zachleat heck, I am now curious around your use case ... you wrote down what you are after, you didn't write down *why* you are after that 🤔
@webreflection maybe a little hard to explain on text but given an arbitrary deeply nested object, I want to add metadata to each property that stores an arbitrary string for that property. the approach I’m using is to convert (for boolean types) primitive booleans to object booleans to add that data and retain the same behavior
@zachleat you’d be better off with a mirror/schema or a descriptor/accessor that returns such metadata instead when you want to
@webreflection @zachleat yeah, like maybe track the contexts with a <context, id> weakmap and use a <`${id}/${prop}`, metadata> map to track the metadata.
@jed @webreflection oh, these are great ideas — thank you! I’ll experiment with it!
I mean, who doesn't?
almost everyone that has read this blog post 😅
@zachleat it's not possible - see https://mastodon.social/@vrugtehagel/115308994476955114
If you want to have metadata for properties, you're probably best off wrapping them with a proxy and using a mechanism (like a prefix or named symbols) to look up the metadata without those properties being included in `Object.keys()` and whatnot.