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

256
Views
How to resolve illegal constructor error when using this as input in constructor?

I have the following error:

TypeError: Illegal constructor
    at new Car (car.js:15:23)
    at main.js:7:13

The car constructor is here and line

constructor(x, y, width, height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;    
        this.controls = new Controls();
        this.sensor = new Sensor(this);
    }

The error is being thrown on this.sensor = new Sensor(this);

The creation of the car object looks like this:

const car = new Car(2, 100, 30, 50);

Any idea why I'm getting this error? I think I should be able to use this as a parameter, so not sure why the error is getting thrown

This is the class Sensor that I'm using:

class Sensor{
    constructor(car){
        this.car = car;
        this.rayCount = 3;
        this.rayLength = 100;
        this.raySpread = Math.PI / 4;
        this.rays = [];
    }

    update(){
        this.rays = [];
        for(let i = 0; i < this.rayCount; i++){
            const rayAngle = lerp(
                this.raySpread / 2, 
                - this.raySpread / 2, 
                i / (this.rayCount - 1)
            );

            const start = {x : this.car.x, y: this.car.y};
            const end = {
                x: this.car.x - Math.sin(rayAngle) * this.rayLength,
                y: this.car.y - Math.cos(rayAngle) * this.rayLength
            };

            this.rays.push([start, end]);
        }
    }

    draw(context) {
        for(let i = 0; i < this.rayCount; i++){
            context.beginPath();
            context.lineWidth = 2;
            context.strokeStyle = "yellow";
            context.moveTo(
                this.rays[i][0].x,
                this.rays[i][0].y
            );
            context.lineTo(
                this.rays[i][1].x, 
                this
            );
            context.stroke();
        }
    }
}
about 4 years ago · Juan Pablo Isaza
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!