0

I have the following code:

class Base { 

  constructor() {
    console.log(this.table)
  }

  static getAll() { 
    console.log("select * from " + this.table);
  }
}

class User extends Base { 
  static table = 'user'
}
class Room extends Base { 
  static table = 'room'
}

let user = new User()
let room = new Room()

User.getAll()
Room.getAll()

output:

undefined
undefined
select * from user
select * from room

the method getAll works and returns "Select * from User" but, the same field is not accessible, at least I could not on the constructor.

how can I access this static field? thank you

David Gueta
  • 69
  • 1
  • 7

1 Answers1

0

You cannot use child props from a parent class. If you wish to use some prop in all child classes then add it to a parent class. Otherwise your class hierarchy has wrong architecture. A base class shouldn't know anything about child classes.

Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • Sorry, what I want is access the child static field "table" from the Base I'm able to access it on the method getAll() but on the constructor, the line: console.log(this.table) returns undefined alsom I don't want to use User, because I will have multiples tables – David Gueta May 10 '20 at 18:51
  • I don't want the base class to know nothing from the Child. When I run User.getAll() it invokes the Base method and even in the base Method, it know the current method is from a User and the this.table works but the same does not happen when I'm creating the instance (probably because this in the constructor has another meaning) – David Gueta May 11 '20 at 09:43