Jump to content

JavaScript Classes Context and "this" keyword


smus

Recommended Posts

I am not familiar with classes, so I need a solution, or, at least, a clue, please!                    

This code causes the error: TypeError: Cannot read property 'vendor' of undefined

When I am trying to check its correctness, the interpreter also informs that the context is not bound to the 'getInfo' method

How can we correct classes in order to avoid the error? 

class Transport {
  constructor(type, price, vendor, model) {
    this.type = type;
    this.price = price;
    this.vendor = vendor;
    this.model = model;
  }

  getInfo() {
    return `${this.vendor}, ${this.model}`;
  }

  getPrice() {
    return this.price + '$';
  }
}

class Car extends Transport {
  constructor(vendor, model, doorsCount, price) {
    super('car', price, vendor, model);
    this.doorsCount = doorsCount;
  }

  getDoorsCount() {
    return `Number of doors: ${this.doorsCount}`;
  }
}

class Bike extends Transport {
  constructor(vendor, model, maxSpeed, price) {
    super('bike', price, vendor, model);
    this.maxSpeed = maxSpeed;
  }

  getMaxSpeed() {
    return `Max speed: ${this.maxSpeed} kmh`;
  }
}

 

Edited by smus
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...