SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to access this object property

    Greetings, I have this simple object where I would like to be able to access "appPath" from "imgFolder". the example below doesn't seem to work. I'm getting "undefined" for this.appPath. is this doable? Thanks.
    Code:
    var appSets = {
        appPath : 'http:mysite.com',
        imgFolder : this.appPath + 'Img/'                   //this does NOT work
    }

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You would have to do this:

    Code javascript:
    var appSets = {
        appPath : 'http:mysite.com',
        imgFolder : appSets.appPath + 'Img/'
    }

  3. #3
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Raffles, I already tried that, I get "appSets has no properties" Any other thoughts?

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that doesn't work either because it's executed before appSets has finished being created.

    You could do this:
    Code:
    var appSets = {
        appPath : 'http://mysite.com/',
        imgFolder : 'Img/',
        getImgFolder : function(){return appSets.appPath + appSets.imgFolder;}
    }
    or this:
    Code:
    var appSets = {
        appPath : 'http://mysite.com/',
        getImgFolder : function(){return appSets.appPath + 'Img/';}
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cbiti View Post
    is this doable?
    Not really, as the object you are creating doesn't exist until it is created. You could use your own constructor function, where you can access the newly created object as "this". Like so:

    Code:
    function AppSet(base) {
      this.appPath = base.appPath;
      this.imgFolder = base.appPath + base.imgPath;
    }
    var appSets = new AppSet({
      appPath : 'http://mysite.com/',
      imgPath : 'Img/'
    });
    alert(appSets.imgFolder);
    This way you also abstract out the a+b logic, so that it only exists once.

    Douglas
    Hello World


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
  •