Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
por qué mi programa no funciona cuando defino y describo el método de un objeto truck.drive()

Tengo una duda sobre el siguiente código, que arroja un error como error de sintaxis, pero la guía que seguí dice que la sintaxis es correcta. No sé qué hacer, no entendí, por favor ayuda.

aquí está el enlace del código:

 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 answers
Answer question

0

Probablemente hayas leído mal. El código equivalente sería:

 // 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!`) }

La asignación a truck.drive es posterior a la inicialización de truck , no dentro del objeto literal.

Primero, no puede poner asignaciones dentro de un objeto literal, solo declaraciones de propiedades. En segundo lugar, no puede hacer referencia a la variable que se está definiendo hasta después de que se haya completado la asignación.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!