我正在做一些事情,在我需要一个数据类型(或者我们称之为hack)的地方有这个特定的需求,它可以具有像对象一样的属性,但是仍然可以被评估为
假
在if语句中。
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.')
}
例如,如果我使用一个对象
{}
,它可以有属性,但是
if(me)
将执行。到目前为止,我已经尝试了以下方法。
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.
这里有人能做这样的黑客吗?