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

163
Views
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 answers
Answer question

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 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!