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

267
Vistas
javascript super, additional properties returning undefined

i was learning about inheritance in a course however i found the super keyword on MDN, i started implementing it, and it sort of works, it will return the two properties that are also in the parent class,but the additional property i try to implement in the child class always returns undefined, how can i impelement this to add properties in the correct way, as i come to understand it which i could be wrong, is super applies the this keyword to the parent class. here's my code...

class Car {
    constructor(speed, make){
        this.speed = speed,
        this.make = make
    }
get speed() {
    return this._speed / 1.6;
}

set speed(mov){
     this._speed = mov * 1.6
}

accel(){
    console.log(this.speed += 5);
}
}

const chevy = new Car(120, 'chevy');

//challenge 3

class Ev extends Car{
    constructor(speed,make,bat){
        super(speed,make)
        this.bat
    }
    billy(){
        console.log('hi');
    }
    chargeBat(charge){
        this.bat = charge;
        return `ur battery is charging to ${charge}`;
    }
    cel(){
        this.speed += 20;
        this.bat - 1;
        return `your car is going ${this.speed} and battery is ${this.bat}`
    }
}
// Ev.protype = Object.create(Car.prototype);

const prius = new Ev(35,'prius',99);
// console.log(prius.chargeBat());
// console.log(prius.cel())
console.log(prius.bat);
console.log(prius.speed)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You throw away the bat value in the Ev constructor. Store it in this.bat to correct that. See code below.

class Car {
    constructor(speed, make){
        this.speed = speed,
        this.make = make
    }
get speed() {
    return this._speed / 1.6;
}

set speed(mov){
     this._speed = mov * 1.6
}

accel(){
    console.log(this.speed += 5);
}
}

const chevy = new Car(120, 'chevy');

//challenge 3

class Ev extends Car{
    constructor(speed,make,bat){
        super(speed,make)
        this.bat = bat;    // <------------- HERE
    }
    billy(){
        console.log('hi');
    }
    chargeBat(charge){
        this.bat = charge;
        return `ur battery is charging to ${charge}`;
    }
    cel(){
        this.speed += 20;
        this.bat - 1;
        return `your car is going ${this.speed} and battery is ${this.bat}`
    }
}
// Ev.protype = Object.create(Car.prototype);

const prius = new Ev(35,'prius',99);
// console.log(prius.chargeBat());
// console.log(prius.cel())
console.log(prius.bat);
console.log(prius.speed)

about 4 years ago · Juan Pablo Isaza Denunciar
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