0

Say i have an following class in javascript

class A {
  constructor(createB){
    if(createB === undefined) this.b = new B();
  }
}

class B extends A {
  constructor(){
    super(false);
  }
}

var global = {};
global.a = new A();

delete global.a;

now what will happen to the instance of B? will that instance of B and its memory automatically destroyed?

codeofnode
  • 18,169
  • 29
  • 85
  • 142
  • Why does `B` inherit from `A`? Does that have something to do with your question? – Bergi Apr 25 '16 at 14:21
  • JS is a garbage-collected language. You don't have to do "memory management". – Bergi Apr 25 '16 at 14:23
  • yes, actually i just want to clear out some doubts when its comes into existence of inheritance. – codeofnode Apr 25 '16 at 14:23
  • Inheritance doesn't change anything about reachability. You still got two distinct objects, regardless what classes they are instances of. – Bergi Apr 25 '16 at 14:24
  • Is that means, deleting a will auto remove b? – codeofnode Apr 25 '16 at 14:24
  • I'm not sure you understood what the `delete` operator does. It removes properties from objects, nothing else. In your case, the `.a` property was the last reference to the `new A` instance, making it eligible for GC as well; but `global.a = null` would've had the same effect. – Bergi Apr 25 '16 at 14:26

0 Answers0