評級組件:自定義rate(1-5)
"★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate);
快速取整
console.log(~~47.11) // -> 47
console.log(~~1.9999) // -> 1
console.log(~~[]) // -> 0
------------
console.log(20.15|0); // -> 20
console.log((-20.15)|0); // -> -20
------------
console.log(20.15^0); // -> 20
console.log((-20.15)^0); // -> -20
------------
console.log(20.15 < < 0); // -> 20
console.log((-20.15) < < 0); //-20
處理比較大的數字時(當數字范圍超出 ±2^31?1 即:2147483647),會有一些異常情況。使用的時候明確的檢查輸入數值的范圍。
SB
(!(~+)+{})[--[~+""][+[]]*[~+[]] + ~~!+]+({}+)[[~!+[]]*~+]
NB
([[]]+)[+!![]]+(+{})[!+[]+!!]
JS處理錯誤
try {
something
} catch (e) {
window.location.href =
"[http://stackoverflow.com/search?q=](https://link.jianshu.com/?t=https://link.zhihu.com/?target=http%3A//stackoverflow.com/search%3Fq%3D)*[js]+" +*
e.message;
}
盒子模型
[].forEach.call($$("*"),function(a){
a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16)
})
整數交換
a ^= b;
b ^= a;
a ^= b;
金錢格式化
正則
var test1 = '1234567890'
var format = test1.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
console.log(format) // 1,234,567,890
非正則
function formatCash(str) {
return str.split('').reverse().reduce((prev, next, index) => {
return ((index % 3) ? next : (next + ',')) + prev
})
}
console.log(formatCash('1234567890')) // 1,234,567,890