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

167
Views
No se puede pasar el objeto como parámetro en el constructor
class Projectile{ constructor(position, velocity){ this.position= position this.velocity = velocity this.radius = 30 } draw(){ c.beginPath() console.log(this.position.x + " " + this.position.y) c.arc(500, 500, this.radius, 0, Math.PI * 2) c.fillStyle = "red" c.fill() c.closePath() } update(){ this.draw() this.position.x += this.velocity.x this.position.y += this.velocity.y } }

Esta es mi clase. He hecho un constructor que toma dos objetos. He usado la siguiente línea de código para crear un nuevo objeto de clase. Debe ser una matriz de objetos.

 const projectiles = [new Projectile({ position: { x:300, y:300 }, velocity: { x:0, y:0 } })]

El problema es que cuando estoy imprimiendo el valor de this.position.x y this.position.y en la consola, se está imprimiendo sin definir. Quiero el valor que pasé al crear el objeto.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

update(){ console.log(this); // position: {position: {...}, velocity: {...}}, velocity: undefined, ... this.draw() this.position.x += this.velocity.x this.position.y += this.velocity.y }

Está pasando un argumento solo a la position del parámetro.

así que deberías cambiar cambiar así

 const projectiles = [new Projectile( // position { x:300, y:300 }, // velocity { x:0, y:0 } )]
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!