0

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.

Matt West
  • 1,376
  • 1
  • 12
  • 18
  • `obj1` is not defined so answer is correct - undefined – msangel Sep 12 '13 at 21:57
  • possible duplicate of [Javascript: Do I need to put this.var for every variable in an object?](http://stackoverflow.com/questions/13418669/javascript-do-i-need-to-put-this-var-for-every-variable-in-an-object) – Bergi Sep 12 '13 at 22:03
  • Right... I changed "var x = obj1.answer" to "var x = testobj1.answer" and it worked fine, but that would only work in that one instance, which kind of defeats the purpose of a constructor. Still, bonehead mistake and you are correct in pointing it out so thank you. – Matt West Sep 12 '13 at 22:05

1 Answers1

0

Heres a working example of what you are trying to do

fiddle: http://jsfiddle.net/yjTXK/1/

var Obj1 = function (){
    this.getResult = function() {
        var result = 5*5;
        return result;
     };

    this.answer = this.getResult();
};

var Obj2 = function(obj1) {
    //assign the answer to this.x, var doesn't 'live' outside of the constructor
    this.x = obj1.answer;
};

//you make an instance of obj1, this is different from the 'class' Obj1
var testobj1 = new Obj1();

//then you pass that instance into an Obj2, so it can be consumed
var testobj2 = new Obj2(testobj1);

console.log(testobj2.x);

W3Schools has quite a good primer on Javascript Objects which should get you familiar with the basics.

Update, why pass in the instance of Obj1 into Obj2?

You need to provide the second instance with a reference to the first. After you create the first instance, you need to let the second object know about it, and that's why you pass it in. This way, you can have a whole bunch of Obj1 instances, and specify exactly which one you want to pass in to Obj2

Robert Byrne
  • 562
  • 2
  • 13
  • Awesome! That's incredibly helpful. What, specifically does passing Obj1 as an argument to Obj2 do? Besides make it work, obviously. – Matt West Sep 12 '13 at 22:00
  • in code in question there is NO constructor parameter for `Obj2` , `obj1` is NOT defining, so, construction FAIL and return undefined – msangel Sep 12 '13 at 22:00
  • @msangel agreed, I'm trying to show how it can be set up to avoid that fail – Robert Byrne Sep 12 '13 at 22:01
  • @user2408170 I'll edit the answer a bit, hopefully provide a bit more clarity – Robert Byrne Sep 12 '13 at 22:02
  • 1
    Thank you Mr. Byrne, you are my hero for the day. The edits you put in made this very clear, and hopefully it will be helpful to people other than just me. – Matt West Sep 12 '13 at 22:08
  • @RobertByrne,so problem is in bad code in question, and i don't understand, what you are trying to fix in your answer. – msangel Sep 12 '13 at 22:08
  • @msangel the OP didn't realise _why_ the code was bad, I'm trying to shed some light on that, not just fix the code – Robert Byrne Sep 12 '13 at 22:09