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?