Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
why my program doesn't work when i define and describe the method of an object truck.drive()

I have a doubt on the following piece of code, which throws an error like syntax error, but the guide which i followed says that the syntax is correct. don't know what to do, failed to understand, please help.

here is the code link:

const car = {
    maker: 'Ford',
    model: 'Fiesta',
  
    drive() {
      console.log(`Driving a ${this.maker} ${this.model} car!`)
    }
  }

car.drive()

// the same above code can be written as:
const bus = {
    maker: 'Ford',
    model: 'Bussie',
  
    drive: function() {
      console.log(`Driving a ${this.maker} ${this.model} bus!`)
    }
  }

bus.drive()

// the same code above can be written in this way:
const truck = {
    maker: 'Tata',
    model: 'Truckie',
  
    truck.drive = function() {
      console.log(`Driving a ${this.maker} ${this.model} truck!`)
    }
  }

truck.drive()

// Now, let us see how the arror function works:
const bike = {
    maker: 'Honda',
    model: 'Unicorn',
  
    drive: () => {
      console.log(`Driving a ${this.maker} ${this.model} bike!`)
    }
  }
  
bike.drive()

Error:

SyntaxError: Unexpected token '.'

at line: truck.drive = function() {
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You probably misread. The equivalent code would be:

// the same code above can be written in this way:
const truck = {
  maker: 'Tata',
  model: 'Truckie'
}

truck.drive = function() {
  console.log(`Driving a ${this.maker} ${this.model} truck!`)
}

The assignment to truck.drive is after the initialization of truck, not inside the object literal.

First, you can't put assignments inside an object literal, only property declarations. Second, you can't refer to the variable being defined until after the assignment has completed.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda