I have a question about class properties. I'll create an example for better understanding. Let's say we have a class.
class Person {}
And we want to add a property. How do we do that? From what I read there are 2 ways:
class Person {myProperty ='Something'} //As shown at https://javascript.info/class. If we use console.log(Person) the property will not show
Now let's create another class that extends Person, let's say Athlete and I want to change myProperty:
class Athlete extends Person{// If we use console.log(Athlete.myProperty ) it will show undefined
myProperty='Something else'// it changes the property
writeSomething() {
console.log(this.myProperty);
}
}
Now let's create a new object using Athlete as constructor
const athlete = new Athlete();
console.log(athlete)// It will have the property myProperty with value 'Something else'
My questions are:
- Where is myProperty stored, I can't find it anywhere inside Person or Athlete?
- Why can I access myProperty inside writeSomething()?
- Why does myProperty appear inside athlete, when it didn't appear anywhere else?
- Is this another way of writing properties as opposed to Person.myProperty or is it something else?
Thank you,
Elanvi
myProperty.