0

I am working on something, and have this specific need where I need a data type (or let's just call it a hack) which can have properties like an object, but can still be evaluated as false in an if statement.

let me = <initialise with a mythical datatype>
me.property = 'value'

console.log(me.property) // Outputs 'value'
if (me){ // Evaluates as false.
    console.log('This should not execute.')
}

For example, if I use an object {}, it can have properties but then if(me) will execute. I have tried the following so far ..

me = ''
me.property = 'value'
// if(me) is false but me.property is undefined.

me = 0
me.property = 'value'
// if(me) is false but me.property is undefined.

me = false
me.property = 'value'
// if(me) is false but me.property is undefined.

me = new String('')
me.property = 'value'
// me.property works but if(me) is true.

me = function(){}
me.property = 'value'
// me.property works but if(me) is true.

me = []
me.property = 'value'
// me.property works but if(me) is true.

Can anyone here craft such a hack?

rmn
  • 1,119
  • 7
  • 18
  • 1
    https://tc39.github.io/ecma262/#sec-toboolean Not long, but TL;DR: if it's an object, it's `truthy`. – Dave Newton Sep 11 '18 at 14:35
  • 1
    if you want a workaroud, i think you really should tell us why you'd want to have this (because that seems like an unrational automatic conversion) ;) – Kaddath Sep 11 '18 at 14:38
  • @Kaddath Unlike... the rest of the JS conversions?! – Dave Newton Sep 11 '18 at 14:40
  • @Kaddath it's a long story, but i have reached a stage where I work a lot on meta programming, than the actual programming itself, and thats what have led me to this conundrum. – rmn Sep 11 '18 at 14:47
  • 1
    @rmn You might want to ask a new question about your [actual problem](https://meta.stackexchange.com/q/66377). Btw, if you are into metaprogramming, I'd recommend to simple create your own language (extension) and use a transpiler - it's easier than you might think, and it allows for hacks like this. – Bergi Sep 11 '18 at 14:53
  • @Bergi I understand what a transpiler is, but what is a language extension? And what might be a good starting point on these? – rmn Sep 11 '18 at 15:02
  • @rmn Something that extends the language to include things that aren't in the language. It can be a completely new language (e.g., CoffeeScript), syntactic extensions (e.g., new constructs like `unless`), pretty much anything. However if your requirement is that only objects of a specific "type" are processed out-of-spec, you're really just going to be interrogating an object property to see if it's the type you want to special-case. IMO the ROI on this is exceedingly low. – Dave Newton Sep 11 '18 at 15:05
  • @rmn I mean an extension to the language, which doesn't change the syntax completely but offers new capabilities - as compared to designing a radically new language and then making it transpile to js. Have a look at https://www.sweetjs.org/ for example – Bergi Sep 11 '18 at 15:06
  • (I was going to bring up Sweet earlier, but without knowing what problem is actually trying to be solved, I couldn't tell if it'd be helpful. I like Sweet.) – Dave Newton Sep 11 '18 at 15:09
  • Thanks guys - Dave and Bergi, for the help. – rmn Sep 11 '18 at 15:13

1 Answers1

2

No, this is not possible.

The only exception to that rule is the document.all object, and it's a host object created by the environment.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Doesn't if (document) also evaluates to true? – rmn Sep 11 '18 at 14:38
  • @rmn Yes, but `Boolean(document.all)` is `false` even though `document.all` is an object with properties – Bergi Sep 11 '18 at 14:40
  • This is good, never knew this, thanks. – rmn Sep 11 '18 at 14:41
  • 1
    I see you have marked this as duplicate, do you think its possible to keep this open for a while to see what sort of hacks people come up with? I am new around here, and can't figure out if I can unmark the question as duplicate myself. – rmn Sep 11 '18 at 14:42
  • 1
    @rmn There is no hack unless you touch the JSVM internals. It's part of the specification (well... sort of). Any implementation that does not honor the spec is already a problem because it would have the potential to break things quite badly. – Dave Newton Sep 11 '18 at 14:44
  • @rmn - i wrote an answer but its been flagged before i could post it. My idea was: `let a = new Object(false); a.property = "a test property"; let b = a.__proto__.valueOf(); console.log('b is: ', b); // output: false console.log('a.property is: ', a.property); // output: "a test property"` – Stuart Sep 11 '18 at 14:46
  • @rmn Sorry, I found the duplicate after I had answered. Maybe I should just delete my answer – Bergi Sep 11 '18 at 14:47
  • @Stuart That's different than testing `a`, which is what the OP asked for. If you're just going to test an object property there's no need to go to such lengths--just put a method on the object to test against. – Dave Newton Sep 11 '18 at 14:48
  • @Stuart `a` and `b` are two distinct values, OP needs a solution with a single thing – Bergi Sep 11 '18 at 14:48
  • @Bergi, your answer is needed, many future souls will be lost without this. So please do not delete your answer, but is there any possibility of keeping it open for a while, like marking it as non-duplicate. – rmn Sep 11 '18 at 14:49
  • 1
    @rmn I could reopen it, but I can guarantee you that you won't get a different answer. The posts on the duplicate explain (and proof) *why* it is impossible. – Bergi Sep 11 '18 at 14:51
  • With your crazy StackOverflow score, who can doubt your judgement :). But maybe just for the sake of [penicillin](https://www.healio.com/endocrinology/news/print/endocrine-today/%7B15afd2a1-2084-4ca6-a4e6-7185f5c4cfb0%7D/penicillin-an-accidental-discovery-changed-the-course-of-medicine), you can keep it open for a couple of days? – rmn Sep 11 '18 at 14:56
  • @rmn ... JS implementations have explicit type conversion requirements in order to be JS. Without those type conversions JS can no longer be expected to work. What you are asking for isn't in the language or any conforming implementation. – Dave Newton Sep 11 '18 at 15:03
  • 1
    [Fun read about `document.all`](https://stackoverflow.com/a/10394873/438992) – Dave Newton Sep 11 '18 at 15:07
  • @DaveNewton that's a really good read. Thank you. – rmn Sep 11 '18 at 15:10