JS短小代碼

評(píng)級(jí)組件:自定義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

處理比較大的數(shù)字時(shí)(當(dāng)數(shù)字范圍超出 ±2^31?1 即:2147483647),會(huì)有一些異常情況。使用的時(shí)候明確的檢查輸入數(shù)值的范圍。

SB

(!(~+)+{})[--[~+""][+[]]*[~+[]] + ~~!+]+({}+)[[~!+[]]*~+]

NB

([[]]+)[+!![]]+(+{})[!+[]+!!]

JS處理錯(cuò)誤

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)

})

整數(shù)交換

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容