tern icon indicating copy to clipboard operation
tern copied to clipboard

Extended classes do not inherit parent member variables.

Open ljuzig opened this issue 6 years ago • 3 comments

Objects created from a class do have all variables defined in the constructor and inside other member functions. When you extend that class, objects created from the extended class do not inherit parent member variables, only functions and getters. The parent variables aren't visible inside the extended class functions too.

-Test case 1: test2 is a Core2 object that extends Core, var1 from Core is not visible screen shot 2018-09-20 at 00 30 08 -Test case 2 (Expected behaviour): test1 is a Core object, var1 from Core is visible screen shot 2018-09-20 at 00 29 56 -Test case 3: Inside the constructor of Core2, var1 from Core is not visible screen shot 2018-09-20 at 00 48 30

Here's the code for testing:

class Core {
  constructor() {
    this.var1 = 1;
  }
  get var2() {
    return 2;
  }
}

class Core2 extends Core { }

const test1 = new Core();
const test2 = new Core2();

test2.

Many thanks!

ljuzig avatar Sep 19 '18 23:09 ljuzig

Can't reproduce case 3

2018-09-20 2 27 53

Case 1 is incorrect because it requires super() in the consturctor of Core2 Case 2 is expected behavior

othree avatar Sep 20 '18 06:09 othree

I found out that Case 3 was a typo in the constructor, sorry! So, as for the first case, I assume that the declaration of the constructor and super() are mandatory for the engine to recognize parent's variables? If I'm not mistaken, when the constructor is not declared, the parent constructor is called automatically.

ljuzig avatar Sep 20 '18 19:09 ljuzig

Yes, but it not easy to do something when not see something. I will keep this open until I found a good solution.

othree avatar Sep 21 '18 01:09 othree