4

The use case I'm considering is creating a hopelessly egocentric Null for coalescing.

This code works but requires an explicit check against the Null in question:

const Null = new Proxy({}, {get: () => Null});

let data = Null;

console.log(data.a.b.c.d !== Null ? "Has value" : "Is Null");

I'd like to be able to leave out the explicit check and simply do

console.log(data.a.b.c.d ? "Has value" : "Is Null");

but that doesn't work because my Null is an object, and as such is treated as truthy.

The spec seems to indicate that this isn't possible, but javascript is such a weird ecosystem that I'm hoping someone will be able to come up with a possible solution.

(Also please don't debate the merits of this kind of Null, I know it's a potentially contentious issue and I'm purely interested in if it CAN be done, not whether it SHOULD be done.)

Siguza
  • 21,155
  • 6
  • 52
  • 89
david
  • 17,925
  • 4
  • 43
  • 57

1 Answers1

2

No, you cannot. An object reference is never "falsy". You can add valueOf() and toString() methods that return false values when they're invoked, but if all that's tested is a reference that won't do any good.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 1
    An immediate counter-example is `document.all instanceof Object`, with `!!document.all === false` but that does seem to be a lone exception that is impossible to emulate. I wasn't really after a < 5 minute "No" as an answer, so I'm going to leave the question open until either someone comes along with a novel method, or the language evolves to a point where it does become possible to do this. – david Jan 16 '17 at 03:45
  • @david what? `document.all` is archaic, and in any browser in this day and age the value will be `undefined`. Maybe IE preserves it, even in Edge, but then it *won't* be `undefined`. I'm not sure I see what you're on about here. In any case, an actual object reference will definitely **never** be false. – Pointy Jan 16 '17 at 03:57
  • @david also `document.all instanceof Object` returns `true` in Firefox, and `!!document.all == false` returns `false`, which makes perfect sense. – Pointy Jan 16 '17 at 04:02
  • Yeah it's archaic, but it's there. Most browsers will preserve it because backwards compatibility is a massive thing in web development. – david Jan 16 '17 at 04:03
  • Interesting, my version of firefox has `!!document.all === false` returning `true`. What version are you checking on? Mine is `52.0a2 (2016-12-06) (64-bit)` – david Jan 16 '17 at 04:04
  • Firefox 50.1.0 returns `true` for `!!document.all === false` because it **is** true. `document.all` is `undefined` in modern Firefox. – Pointy Jan 16 '17 at 04:07
  • I thought you said `document.all instanceof Object` returned `true` in your browser? `undefined instanceof Object` would return `false`. – david Jan 16 '17 at 04:13
  • No, in Firefox v50 `document.all` is `undefined`, and `undefined` is not an instance of `Object`. – Pointy Jan 16 '17 at 04:34
  • The fact that `document.all instanceof Object` returns `true` probably has something to do with what happens to `undefined` on the left side of `instanceof`. It's `undefined` because Firefox doesn't implement it. If it ends up being an instance of 'Object` it's because something turns it into `null` I bet. – Pointy Jan 16 '17 at 04:38