Javascript 我应该如何使用JS?
我对这个脚本有一个问题,但我不知道控制台在哪里对我说:uncaughtsyntaxerror:uncontractedtoken'{' 但我不知道为什么一切看起来都很好,我看到了一些关于JS的课程,代码看起来是一样的,所以我不明白哪里是错的。 此代码适用于某些按钮Javascript 我应该如何使用JS?,javascript,Javascript,我对这个脚本有一个问题,但我不知道控制台在哪里对我说:uncaughtsyntaxerror:uncontractedtoken'{' 但我不知道为什么一切看起来都很好,我看到了一些关于JS的课程,代码看起来是一样的,所以我不明白哪里是错的。 此代码适用于某些按钮 const-celeste=document.getElementById('celeste') const violeta=document.getElementById('violeta') const naranja=docu
const-celeste=document.getElementById('celeste')
const violeta=document.getElementById('violeta')
const naranja=document.getElementById('naranja')
const verde=document.getElementById('verde'))
const btnEmpezar=document.getElementById('btnEmpezar')
Juego类{
构造函数(){
这个.inicializar()
这是通用安全()
}
inicializar(){
btnEmpezar.classList.add('hide')
this.nivel=1
这是一个颜色={
塞莱斯特,
维奥莉塔,
纳兰贾,
佛得角
}
}
一般保障(){
this.secuencia=新数组(10.fill(0.map)(n=>Math.floor(Math.random()*4))
}
siguienteNivel(){
TransformarNumeroColor(数字){
开关(数字){
案例0:
返回“celeste”
案例1:
返回“violeta”
案例3:
返回“naranja”
案例4:
返回“verde”
}
}
iluminarSecuencia(){
对于(var i=0;ithis.iluminarColor(颜色),1000*i)
}
}
iluminarColor(彩色){
this.colors[color].classList.add('ligth'))
setTimeout(()=>this.apagarColor(color),350)
}
彩色公寓{
this.colors[color].classList.remove('light')
}
}
}
函数empezarJuego(){
this.juego=new juego()
}
当您声明siguientenievel()
方法时,出现语法错误。我认为您希望创建一个顺序动作链,但正确的方法是在自己的方法中定义它,并根据需要调用函数
下面是我如何构建这个类的,我只是调整了顺序
<script>
const celeste = document.getElementById('celeste')
const violeta = document.getElementById('violeta')
const naranja = document.getElementById('naranja')
const verde = document.getElementById('verde')
const btnEmpezar = document.getElementById('btnEmpezar')
class Juego {
constructor() {
this.inicializar()
this.generarSecuencia()
}
inicializar() {
btnEmpezar.classList.add('hide')
this.nivel = 1
this.colores = {
celeste,
violeta,
naranja,
verde
}
}
generarSecuencia() {
this.secuencia = new Array(10).fill(0).map(n => Math.floor(Math.random() * 4))
}
siguienteNivel() {
//Make different calls as you need
let dummyNumber
transformarNumeroAColor(dummyNumber)
}
transfromarNumeroAColor(numero) {
switch (numero) {
case 0:
return 'celeste'
case 1:
return 'violeta'
case 3:
return 'naranja'
case 4:
return 'verde'
}
}
iluminarSecuencia() {
for (var i = 0; i < this.nivel; i++) {
var color = this.transformarNumeroAColor(this.secuencia[i])
setTimeout(() => this.iluminarColor(color), 1000 * i)
}
}
iluminarColor(color) {
this.colores[color].classList.add('ligth')
setTimeout(() => this.apagarColor(color), 350)
}
apagarColor(color) {
this.colores[color].classList.remove('light')
}
}
function empezarJuego() {
this.juego = new Juego()
}
const celeste=document.getElementById('celeste')
const violeta=document.getElementById('violeta')
const naranja=document.getElementById('naranja')
const verde=document.getElementById('verde'))
const btnEmpezar=document.getElementById('btnEmpezar')
Juego类{
构造函数(){
这个.inicializar()
这是通用安全()
}
inicializar(){
btnEmpezar.classList.add('hide')
this.nivel=1
这是一个颜色={
塞莱斯特,
维奥莉塔,
纳兰贾,
佛得角
}
}
一般保障(){
this.secuencia=新数组(10.fill(0.map)(n=>Math.floor(Math.random()*4))
}
siguienteNivel(){
//根据需要打不同的电话
让dummyNumber
transformarNumeroAColor(dummyNumber)
}
TransformarNumeroColor(数字){
开关(数字){
案例0:
返回“celeste”
案例1:
返回“violeta”
案例3:
返回“naranja”
案例4:
返回“verde”
}
}
iluminarSecuencia(){
对于(var i=0;ithis.iluminarColor(颜色),1000*i)
}
}
iluminarColor(彩色){
this.colors[color].classList.add('ligth'))
setTimeout(()=>this.apagarColor(color),350)
}
彩色公寓{
this.colors[color].classList.remove('light')
}
}
函数empezarJuego(){
this.juego=new juego()
}