There is no general way for a child object to know that it is a member of the parent object. In your situation, you can directly reference the parent in displayA() , for example:
displayA : function(){ console.log(parent.a); }
You do not need to place the parent in the global scope and use window.parent as the other answer suggests; since you declare displayA within parent , the function will close over parent , and it will be available anywhere inside child . Since the closure contains a reference to the parent object, you will see that changes to parent will be reflected in the behavior of displayA . For example, suppose parent and child defined as in your example, except for displayA , to use parent.a . Then:
parent.child.displayA();
All this, if you are trying to simulate OOP, then the other answer is right: you should learn more about how the Javascript prototype chain works.
Matt wetmore
source share