I've been working on JavaScript the last few months, and I'm trying to get a deeper understanding of objects. The following problem is giving me fits. Rather that spell it out I'll just give a code example:
var Obj1 = function (){
this.getResult = function() {
var result = 5*5;
return result;
};
this.answer = this.getResult();
};
var Obj2 = function() {
var x = obj1.answer;
};
var testobj1 = new Obj1();
var testobj2 = new Obj2();
console.log(testobj2.x);
This returns "undefined." I have two questions: The first is "why?" the second is "How could I make this work?" I'd like to be able to access the answer method of testobj1 from inside testobj2. Is there a way? Any links that would educate me on the principle I'm not understanding here are much appreciated.
PS - I did my due diligence searching Google and this site for the answer to my question. If I found it I didn't understand that I had, so any new explanations are welcome.