0

I am new to js class concept.I am trying to learn about it .Here i have a problem for you guys .when ever i declare a constructor function inside child class i get a

uncaught referenceError: This is not defined error

if i can not define a constructor inside child class then how i can instantiate private property of child class inside class body?

class Parent{
   constructor(){
       this.parent='i am parent';
       this.realactivity='do nothing :p';
   }
   activity(){
        console.log(this.realactivity);
   }
}

class Child extends Parent{
    constructor(){
      this.realactivity='do not listen to my parents :p';
    }
    activity(){
        super.activity();
        console.log('obeying the parent');
    }
}
var child1=new Child();
child1.activity();
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • You are trying to make a class with javascript? javascript is not java... here, try like this: http://www.phpied.com/3-ways-to-define-a-javascript-class/ – Giovanni Perillo Mar 16 '16 at 17:52
  • 1
    @Giovanni: Welcome to ES2015! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes – Felix Kling Mar 16 '16 at 17:52
  • 1
    It is in ES6: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes – millerbr Mar 16 '16 at 17:52
  • 1
    @GiovanniPerillo: _javascript is not java._ ; and thank goodness for that... – dandavis Mar 16 '16 at 17:53
  • 1
    The error message references `This`, not `this`. Are you sure you spelled `this` correctly? – Jacob Krall Mar 16 '16 at 17:53
  • 1
    re:`how i can instantiate private property of child class inside class body`? you can't; `class` is basically setting up the prototype, not the own properties. – dandavis Mar 16 '16 at 17:55
  • You need to add `super()` inside the child constructor: `constructor() { super(); this.realactivity='do not listen to my parents :p'; }` – Gasim Mar 16 '16 at 17:58

0 Answers0