JS 让表达式(a ==1 && a == 2 && a == 3)成立

valueOf

var a = { 
    value: 1
    , valueOf: function() {
        return this.value++;
    }
}

Symbol.toPrimitive

var a = { 
    value: 1
    , get[Symbol.toPrimitive]() {
        return () => this.value++;
    }
}

defineProperty

Object.defineProperty(window, 'a', {
    get: function(){ 
        this.value ? this.value++ : this.value = 1; 
        return this.value;
    }
})

toString

var a = {
    value:1
    ,toString: function(){        
        return this.value++
    }
}

to String & shift

var s = [1, 2, 3];
s.toString = s.shift;

 

 
posted @ 2024-05-17 15:10  Robot-Blog  阅读(5)  评论(0编辑  收藏  举报