Javascript is insane
Mar. 15th, 2013 06:58 pm'' == '0' // false 0 == '' // true 0 == '0' // true !0 == true // true !1 == false // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true " \t\r\n" == 0 // true NaN === NaN // false !!NaN === !!NaN // trueI recently saw someone prefacing their code with
var t=!0;... Because they wanted to save space in their code by replacing every occurrence of true with t, and also wanted so save two bytes while declaring what t was.
And I thought Perl invited abuse!
If you see anyone doing this, pour your drink on their head.
no subject
Date: 2013-03-16 03:18 am (UTC)In the interest of creating human-readable and maintainable code, such hacks should only be applied by machines in a pre-processing phase. (Such as JS minify'rs. "!0" is something that a smart JS compressor ought to handle for you.)
Recently I found a JS compressor that builds an array of every alphanumeric word used in your code, then packs the code as a series of digits and performs a massive search-and-replace-and-then-eval() at runtime.
Also: The PHP truth table.
Also: All languages are terrible in specific ways.
</rant>
no subject
Date: 2013-03-16 03:23 am (UTC)no subject
Date: 2013-03-16 03:52 am (UTC)Seems it would make JS compression a tool only for code obfuscators...
Speaking of which, I'm learning all about the fun to be had by replacing "if" statements with the "&&" operator, and using the Javascript comma to chain together statements for no good reason, by reading obfuscated code:
Wr.prototype.K = function(a,b) { if(!this.tg) { var c=a.ea(),d=a.ya().lc(),e,f=a.ib(); e=Xr(d); e = 90 == e ? d.kd(f.$q(),c) : 180 == e ? d.kd(f.ah(),c) : 270 == e ? d.kd(f.Ot(),c) : d.kd(f.bh(),c); var g = Xr(d); var f = 90 == g ? d.kd(f.Ot(),c) : 180 == g ? d.kd(f.bh(),c) : 270 == g ? d.kd(f.$q(),c) : d.kd(f.ah(),c); var g = d.bj(c); var l = g/2; var p = Yr(e,f,g); var p = ((Fca * p || 256) - p) / 2; p > l && (p=l); var q = e.y - f.y; var q = ((Fca * q || 256) - q) / 2; q > l && (q=l); e.x -= p; e.y += q; f.x += p; f.y -= q; 0 > f.y && (f.y=0); e.y > g && (e.y=g); l = Xr(d); if (90 == l || 270 == l) 0 > e.x && (e.x = 0), f.x > g && (f.x = g); for (; 0 > e.x; ) e.x += g; for (; f.x > g; ) f.x -= g; e.x == f.x && (e.x = 0, f.x = g); p = Yr(e,f,g); q = e.y - f.y; 2048 <= p && (g = (p - 2048) / 2 + 1, e.x += g, f.x -= g); 2048 <= q && (g = (q - 2048) / 2 + 1, e.y -= g, f.y += g); g = Xr(d); l = d.bj(c); l = Yr(e,f,l); q = e.y - f.y; p = new J(e.x,e.y); 90 == g ? p.x += l : 180 == g ? (p.x += l, p.y -= q) : 270 == g && (p.y -= q); g = d.Fg(p,c); l = Xr(d); q = d.bj(c); q = Yr(e,f,q); e = e.y - f.y; f = new J(f.x, f.y); 90 == l ? f.x -= q : 180 == l ? (f.x -= q, f.y += e) : 270 == l && (f.y += e); e = d.Fg(f,c); e = new Aa(g,e); sma(this, e, c, d, b) } };no subject
Date: 2013-03-16 12:25 pm (UTC)"NaN == NaN" is false by IEEE standard. However, the whole idea of "!NaN" (logical negation of NaN?!) is crazy.