我正在做某事,并且在需要数据类型(或称其为hack)的地方有这种特殊需要,该数据类型可以具有对象的属性,但仍可以被评估为 false 在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.
这里的任何人都可以制作这样的骇客吗?