-2

I am wondering why I cannot retrieve the value in the alert function. I know the 'mainObj' is being created as I can test with an alert within the object constructor. but I can not access the child object properties. Can someone explain what I'm doing wrong? Here is the code:

//used as a global
var theOBJ;

//child objects
function objChild(n,o,bgc){
    this.name = n;
    this.orientation = o;
    this.backgroundcolor = bgc;
}

//the Main Object
function objMain(n,o,bgc,ns){
    this.name = n;
    this.numsides = ns;
    if(this.numsides>2||this.numsides<1){
      alert("Number of sides are incorrect, setting to '1'"); 
      numsides=1;
    }
    child1 = new objChild(n+"_child1",o,bgc);
}


function createNewObject(n){
    //create a new Object
    theOBJ = new objMain(n,"landscape","",3);

    alert(theOBJ.child1.name);
}
michael
  • 15
  • 6
  • 1
    Where's the rest of the code? This just defines a variable and three functions. – melpomene Sep 10 '15 at 18:40
  • You are constructing.. you need to initialize ! – Onilol Sep 10 '15 at 18:41
  • well there is a lot of code. but this is all that is relevant – michael Sep 10 '15 at 18:42
  • is this not where the object gets initialized? theOBJ = new objMain(n,"landscape","",3); – michael Sep 10 '15 at 18:43
  • sorry,im not being smart with anyone. I really that that sending the parms with the function call and new keyword would initialize. – michael Sep 10 '15 at 18:45
  • @michael it is. You just forgot something. – Loïc Faure-Lacroix Sep 10 '15 at 18:46
  • @michael If that is all the relevant code, then it can be shortened to nothing. That code literally does nothing. – melpomene Sep 10 '15 at 18:49
  • @Onilol as you can see, constructing actually initialize an object. – Loïc Faure-Lacroix Sep 10 '15 at 18:49
  • @melpomene it takes a few seconds to find the problem here. – Loïc Faure-Lacroix Sep 10 '15 at 18:50
  • @LoïcFaure-Lacroix If you make certain assumptions, yes. – melpomene Sep 10 '15 at 18:52
  • @LoïcFaure-Lacroix He defined and declared, he didn't initialize. There are no function calls :) Check this : http://stackoverflow.com/questions/23345554/the-differences-between-initialize-define-declare-a-variable – Onilol Sep 10 '15 at 18:56
  • well there has to be something wrong with the child1 initialization. I can remark that out and retrieve the name property of the main object in the alert. – michael Sep 10 '15 at 19:15
  • Well, I'm more confused now. This worked as an assignment within the parent object: this.child1 = new objChild(n+"_child1",o,""); Then in the objChild object I added: alert("initialized"); The alert from the createNewObject function then returned the theOBJ.child1.name value. But I still do not understand why I need the alert in the objChild definition to push the functionality thru. – michael Sep 10 '15 at 19:48
  • @Onilol he's calling the methods inside the `createNewObject` – Loïc Faure-Lacroix Sep 11 '15 at 06:00
  • @LoïcFaure-Lacroix there is no function **call** there , man.. He merely declared the var and the functions. – Onilol Sep 11 '15 at 14:12
  • @Onilol the call to the constructor is in the `createNewObject` and how the `createNewObject(n)` is called doesn't matter in this case. Don't be so picky on a new user. – Loïc Faure-Lacroix Sep 11 '15 at 18:37
  • Thank you for your concerns. at this point there is just a "class" with no methods. I'm trying to organize my thoughts using a new language. I'm just not uncerstanding some things. Thanks for all the input. I'm sorry for confusing you with just the bare bones. – michael Sep 11 '15 at 19:21

1 Answers1

1

Because you're not assigning the child1 object to the this object in the objMain constructor. As it is defined without using var or being directly affected to this it could polute the global scope.

It should be :

 function objMain(n,o,bgc,ns){
    this.name = n;
    this.numsides = ns;
    if(this.numsides>2||this.numsides<1){
      alert("Number of sides are incorrect, setting to '1'");
      // <<< this line also needs a change >>>
      this.numsides=1;
    }
    // <<< this line >>>
    this.child1 = new objChild(n+"_child1",o,bgc);
 }
Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99