4 ms·
This is what I came up with: var i = 0; window.__defineGetter__("a", function() { i++; return i; }); if(a === 1 && a === 2 && a === 3) { "Wtf"; }
by _Microft 9y ago
This is what I came up with:
var i = 0;
window.__defineGetter__("a", function() { i++; return i; });
if(a === 1 && a === 2 && a === 3) {
"Wtf";
}
- Sephr 9y agoAlso works with valueOf: let i = 0, a = {valueOf: () => ++i}; if (a == 1 && a == 2 && a == 3) { console.log("all equal"); } Object.defineProperty (modern version of __define[GS]etter__): let i = 0; Object.defineProperty(self, "a", {get: () => ++i}); if (a == 1 && a == 2 && a == 3) { console.log("all equal"); }
- _Microft 9y agoMix it with a pseudo-random number-generator for extra fun. var i = 0; window.__defineSetter__("x", function(value) { i = value; }); window.__defineGetter__("x", function() { // LCG, a = 4, c = 1, m = 9; i = (4 * i + 1) % 9; return i; }); x = 3; if(x === 4 && x === 8 && x === 6) { "wtf!"; }