Search code examples
javascriptprivate-members

How to declare a private member in js classes and extend it to other classes?


Good Evening. I want to create a parent class vehicle with a private member #manufacturer. I want to extend class vehicle to class car and motorcycle. I declared a const d = new car. If i try to acces to manufacturer by console.log(d.manufacturer) i recive and undefined.

// task 1

class vehicle {
  #manufacturer;
  name;

  constructor(manufacturer, name) {
    this.#manufacturer = manufacturer;
    this.name = name;
  }

  get name() {
    return this.name;
  }
}

class car extends vehicle {
  #type;
  constructor(manufacturer, name, type) {
    super(manufacturer, name);
    this.#type = type;
  }

  get type() {
    return this.#type;
  }

  set type(value) {
    if (value.length > 3) this.#type = value;
  }
}

class motorcycle extends vehicle {
  motortype;
  constructor(manufacturer, name, motortype) {
    super(manufacturer, name);
    this.motortype = motortype;
  }

  get motortype() {
    return this.motortype;
  }

  set motortype(value) {
    if (value.length > 3) {
      this.motortype = value;
    }
  }
}

const e = new motorcycle('audi', 'a3', 'sport');
console.log(e.motortype);
e.motortype = 'supersport';
console.log(e.motortype);

const d = new car('bmw', 'm2', 'cool');
console.log(d.type);
d.type = 'lazy';
console.log(d.type);
console.log(e.name);
console.log(e.motortype);
console.log(d.manufacturer)

I tried to change the constructor in order to fix this problem by putting as well an #manufacturer in the constructor. But i recive an error.


Solution

  • Private properties aren't inherited thus with a car object (subclass) you can't access a private member defined in vehicle (superclass). But you can make the private member accessibile with a public get method like this:

    class vehicle {
      #manufacturer;
      name;
    
      constructor(manufacturer, name) {
        this.#manufacturer = manufacturer;
        this.name = name;
      }
    
      get name() {
        return this.name;
      }
      
      get manufacturer(){
      return this.#manufacturer;
    }
    
    }
    
    class car extends vehicle {
      #type;
      constructor(manufacturer, name, type) {
        super(manufacturer, name);
        this.#type = type;
      }
    
      get type() {
        return this.#type;
      }
    
      set type(value) {
        if (value.length > 3) this.#type = value;
      }
    }
    
    class motorcycle extends vehicle {
      motortype;
      constructor(manufacturer, name, motortype) {
        super(manufacturer, name);
        this.motortype = motortype;
      }
    
      get motortype() {
        return this.motortype;
      }
    
      set motortype(value) {
        if (value.length > 3) {
          this.motortype = value;
        }
      }
    }
    
    const e = new motorcycle('audi', 'a3', 'sport');
    console.log(e.motortype);
    e.motortype = 'supersport';
    console.log(e.motortype);
    
    const d = new car('bmw', 'm2', 'cool');
    console.log(d.type);
    d.type = 'lazy';
    console.log(d.type);
    console.log(e.name);
    console.log(e.motortype);
    console.log(d.manufacturer)