How to access variable of one class from another class?

There is a suspicion (strong) that I do not understand too well the classes.

Suppose there is such a program.
https://codepen.io/marisol.Cartwright/pen/jObqMrx?editor...

The question is: how do I class SecondClass reach importantVar, which is in FirstClass?
April 19th 20 at 12:23
1 answer
April 19th 20 at 12:25
Solution
what I do not understand too well the classes

Then you obviously need unasledovala, read about the extends.
class FirstClass {
 constructor() {
 this.importantVar = '1234asd'
}

 someFunction() {
alert(this.importantVar);
}
}

class SecondClass extends FirstClass {
 constructor(importantVar) {
super();
 this.justAnotherVar = 'ooooo';
}

 iWannaImportantVar() {
alert(this.importantVar);
}
}

gasgasgas function() {
 const firstClass = new FirstClass();
 const secondClass = new SecondClass();

firstClass.someFunction();
secondClass.iWannaImportantVar();
}

gasgasgas();
Thank you for the way. But there is another question: if I, for example, want in FirstClass to change this variable, then in SecondClass it will not change. As far as I know, this inheritance creates two unrelated variables and the change in FirstClass will not change anything in SecondClass. But what if you need to change it in FirstClass, however, and in SecondClass? - marisol.Cartwright commented on April 19th 20 at 12:28
@marisol.Cartwright, I do not quite understand your question, you mean if you change the value 1234asd to say qwerty in FirstClass everything will be okay in the SecondClass but it doesn't change? or did you mean to change the name of variables? if you change the name in the FirstClass and SecondClass have to give a precise name to the second class was able to find such a variable, since we inherited.
To such questions I recommend to watch it. - Loyc commented on April 19th 20 at 12:31
@Loyc, Yes, I meant the first option. If in the first class change 1234asd on qwerty, then in second grade, nothing will change.
Thanks for the video, look. - marisol.Cartwright commented on April 19th 20 at 12:34
@marisol.Cartwright, I probol I have changed, you are not quite right - Loyc commented on April 19th 20 at 12:37
@Loyc, well, you see, I with classes in General are bad. I will try what to do. Many thanks to You personally for the help! - marisol.Cartwright commented on April 19th 20 at 12:40

Find more questions by tags JavaScript