function getRandomColor(){ let colorStr = Math.ceil(0xffffff*Math.random()).toString(16); return '#' + colorStr.padStart(6,'0'); }
這個函數中值得注意的是:dom
1.js中0x開頭表示16進制函數
2.toString(x)中x能夠是任意除1之外正數,分別x進制格式輸出spa
3.str.padStart(num,char)是es7的方法 code
colorStr.padStart(6,'0')能夠被替換成 '0'.repeat(6-colorStr.length)+colorStr