setInterval(function () { console.log("z"); }, 100); // before setInterval('console.log("z")', 100); // after
Variables
var o = {}; // Object literal var a = []; // New Array var r = /.*/; // New Regex var s = "" + 0; // Convert to string var n = +"7"; // Convert to number (7) var b = !!b; // Converts to a boolean var f = ~~3.434; // Same as Math.floor(3.434) var g = -~3.434; // Same as Math.ceil(3.434) var x = 5e3; // Same as 5000 var c = c || z; // Coalesce, if c is null then set it to z. "abcde"[1]; // charAt shorthand, results in 'b'. +newDate(); // Shorthand for (new Date()).getTime(); Date.now(); // Even shorter shorthand for the above var a = x ? y : z; // Ternary operator, short for: var a;if(x){a=y;}else{a=z;} !0; // Shorthand for true !1; // Shorthand for false void0; // Shorthand for undefined
隐式转换
a = "30"; b = "10"; c = a + b; // failure c = parseInt(a) + parseInt(b); // too long
c = -(-a - b); // try these c = ~~a + ~~b; c = +a + +b; c = a - -b;
运算符
hasAnF = "This sentence has an f.".indexOf("f") >= 0; // before hasAnF = ~"This sentence has an f.".indexOf("f"); // after // ==================================
// Longhand if (str.indexOf(ndx) == -1) { // Char not found }
// Shorthand if (~str.indexOf(ndx)) { // Char not found. } //===========================================
rand10 = Math.floor(Math.random() * 10); // before rand10 = 0 | (Math.random() * 10); // after // ~ 优先级低于|
//=========================================== Math.floor(a / 2); // before a >> 1; // after
Math.floor(a / 4); // before a >> 2; // after //==========================================
million = 1000000; // before million = (1e6)[ // after //==========================================
(Infinity, -Infinity) ][(1 / 0, -1 / 0)]; // before // after //=============================================
if (isFinite(a)) if (1 / a) // before // after //==========================================
//使用当前日期生成随机整数 newDate() & 1; // Equivalent to Math.random()<0.5 newDate() % 1337; // Equivalent to Math.floor(Math.random()*1337))
i = 0 | (Math.random() * 100); // before i = newDate() % 100; // after //=============================================
string
html = "<a href='" + url + "'>" + text + "</a>"; // before html = text.link(url); // after //'abc'.link('www.baidu.com') -> '<a href="www.baidu.com">abc</a>' //==================================================
// Longhand "myString".charAt(0);
// Shorthand "myString"[0]; // returns 'm'
//Pretty useful for RGB declarations. "rgb(" + (x + 8) + "," + (y - 20) + "," + z + ")"; // before "rgb(" + [x + 8, y - 20, z] + ")"; // after
"rgb(255," + (y - 20) + ",0)"; // before "rgb(255," + [y - 20, "0)"]; // after
条件语句
// Longhand var big;
if (x > 10) { big = true; } else { big = false; }
// Shorthand var big = (x > 10) ? true : false;
var big = (x > 10);
//further nested examples var x = 3, big = (x > 10) ? 'greater 10' : (x < 5) ? 'less 5' : 'between 5 and 10';
console.log(big); // "less 5"
var x = 20, big = { true: x > 10, false : x< =10 };
//Longhand if (x == a) { x = b; } elseif (x == b) { x = a; } // x = 1, a = 1, b = 2 // 1st run: x = 2 // 2nd run: x = 1 // 3rd run: x = 2 // 4th run: x = 1 // ...
// Shorthand x = a ^ b ^ x; //====================================================