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

170
Views
Unable to pass the object as parameter in 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
    }
}

This is my class. I have made a constructor that takes two objects. I have used following line of code to make a new object of class. It should be an array of objects.

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

The problem is when I am printing the value of this.position.x and this.position.y in console , it is printing undefined. I want the value that I have passed while creating the object.

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
}

You are passing an argument only to the parameter position.

so you should change change like this

 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!