View Full Version : Creating custom objects more than 1 level deep

12-05-2006, 11:46 AM
Ok, I'm having a tough time understanding how to create a custom object in JavaScript that's more than just one level deep. The relationship in the various levels just confuses the heck out of me. Lets take the below example I whipped up. Please ignore design, which is poorly thought out:

function mybuilding(){



var b1=new mybuilding()
var r1=new b1.rooms()

alert(r1.totalrooms+" "+b1.totalrooms) //How do I get it to alert "5" and "5"?

Here I'm hoping to define an object with 2 levels:

mybuilding() //constructor
mybuilding.rooms() //constructor?
mybuilding.rooms.changeTo() //method

with change to "this.roomcount" at all levels affecting only the one that's binded to the root constructor object. In other words, so the above example alerts "5" and "5", not "5" and "3". I don't understand the mechanism behind prototype, especially when it's used on top of another prototype property. Also, when I define a constructor function onto of the main constructor, what happens exactly in terms of scope? Is it possible to actually be able to use the syntax:


instead of having to use the keyword "new" twice to get to the second level?