Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda