Results 1 to 1 of 1

Thread: Creating custom objects more than 1 level deep

  1. #1
    Join Date
    Dec 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating custom objects more than 1 level deep

    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:

    Code:
    function mybuilding(){
    this.totalrooms=3
    }
    
    mybuilding.prototype.rooms=function(){
    }
    
    mybuilding.prototype.rooms.prototype.changeTo=function(total){
    this.totalrooms=total
    }
    
    var b1=new mybuilding()
    var r1=new b1.rooms()
    r1.changeTo(5)
    
    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:

    Code:
    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:

    Code:
    mybuilding.rooms.changeTo()
    instead of having to use the keyword "new" twice to get to the second level?
    Last edited by project2007; 12-05-2006 at 11:53 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •