Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda