一行代碼搞定一個評級組件
let rate = r => "★★★★★☆☆☆☆☆".slice(5 - r, 10 - r);
雖然是有裝逼的嫌疑纷闺,但是的確是很實(shí)用有沒有。
生成隨機(jī)字符串
Math.random().toString(16).substring(2);
Math.random().toString(32).substring(2);
取整
~~ 3.1415926
3.1415926 | 0
3.1415926 >> 0
格式化金錢
function formatMoney (money) {
if(!money) return 0
var temp = money.toString().split('.')
var int = temp[0].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
var float = temp[1] ? (money-temp[0]).toFixed(2) : ''
return int + float.substring(1)
}
自己寫的渣渣版本卓舵,勉強(qiáng)能用枯跑。
從數(shù)組中取最大/小值
function findNumber (arr, flag) {
if (flag) { // max
return Math.max.apply(Math, arr)
} else { // min
return Math.min.apply(Math, arr)
}
}
arguments轉(zhuǎn)換為數(shù)組
argument是一個類數(shù)組,有時候需要將其轉(zhuǎn)換為數(shù)組使用
arguments = Array.prototype.slice.call(arguments)
arguments = Array.from(arguments)
???拾并?
Array.prototype.forEach.call(document.querySelectorAll('*'),
dom => dom.style.outline = `1px solid #${parseInt(Math.random() *
Math.pow(2,24)).toString(16)}`)
你要問我這有啥作用, 請復(fù)制打開控制臺 唱跳rap籃球+v鹏浅,回車你就知道了嗅义。