function is a reserved word ?如何解决,求高手帮忙
function getRandomColor(){
let rgb = []
for(let i=0;i<3;i++){
let color = Math.floor(Math.random()*256).toString(16)
color = color.length==1?'0'+color:color
rgb.push(color)
}
return '#'+rgb.join('')
}
//以下为错误提示
VM4193:1 file: pages/index/index.js
unknown: function is a reserved word (118:2)
116 | },
117 |
> 118 | function getRandomColor(){
| ^
119 | let rgb = []
120 | for(let i = 0;i<3; i++){
121 | let color = Math.floor(Math.random() * 256).toString(16)
改成以下方式可以表达,但是反馈的颜色一直是白色,无法随机
//生成随机颜色
getRandomColor: function (){
let rgb = [ ]
for ( let i = 0; i< 3; ++i ) {
let color = Math.floor(Math.random() * 256).toString(16)
color = color.length == 1 ? '0' + color:color
rgb.push(color)
}
return '#' + rgb.join(' ')
}