1

Possible Duplicate:
How can a Javascript object refer to values in itself?

I have some JS as follows:

var Wrap = {

    Inner : {

        site_base : "http://site.com/",
        marker_purple : site_base + "images/icon/marker-puple.png"
    }
}

site_base is undefined. Wrap.Inner.site_base is undefined.

How can I access my values within the same object?

Community
  • 1
  • 1
Alex
  • 8,353
  • 9
  • 45
  • 56
  • 3
    You can't. You'd have to do that in a separate statement: `Wrap.Inner.marker_purple = Wrap.Inner.site_base + ...` – Pointy Oct 25 '12 at 14:49
  • 1
    Nope; http://stackoverflow.com/questions/2787245/how-can-a-javascript-object-refer-to-values-in-itself – Alex K. Oct 25 '12 at 14:49
  • 1
    Object literals do not define a scope, functions do for variables. – Bergi Oct 25 '12 at 14:52

6 Answers6

3

The error you have is due to the fact that site_base is undefined, so object creation fails.

Try this:

var site_base = "http://site.com/";

var Wrap = {
    Inner : {
        site_base : site_base,
        marker_purple : site_base + "images/icon/marker-puple.png"
    }
}
Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81
2

Alternative to a separate statement:

var Wrap = { // variable names starting with capital letters make me uncomfortable
  Inner: function() {
    var base = "http://site.com/";
    return {
      site_base: base,
      marker_purple: base + "images/icon/marker-purple.png"
    };
  }()
};
Pointy
  • 405,095
  • 59
  • 585
  • 614
0

You have to do it in different statements as the object does not exist yet.

var Wrap = {
    Inner:{
        site_base: "http://site.com/"
    }
};
Wrap.Inner.marker_purple = Wrap.Inner.site_base + "images/icon/marker-puple.png"
ferrants
  • 601
  • 6
  • 11
  • I know what you mean, but "on different lines" doesn't sit well with me. Although it probably should, given the auto statement termination nature of JavaScript engines in general. – Grant Thomas Oct 25 '12 at 14:50
0

Use a closure within an Immediately Invoked Function Expression:

var Wrap = (function(){
  var site_base = "http://site.com/"
  return {
      Inner: {marker_purple : site_base + "images/icon/marker-puple.png"}
  };
}());
KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

As already stated in the other answers, you can not access object properties, before the object is actually created. Thus your code fails.

When using ECMAScript 5 and the Object.create() function, however, you can mimic the behavior you want by using the get() function like this:

var Wrap ={
  Inner: Object.create(Object.prototype, {

    site_base: { writable:true, configurable:true, value: "http://site.com/" },

    marker_purple: {
      get: function() { return this.site_base + "images/icon/marker-puple.png" }
    }
  })
};

More information can be found on MDN, e.g. on browser support

Christoph
  • 50,121
  • 21
  • 99
  • 128
Sirko
  • 72,589
  • 19
  • 149
  • 183
0

Is it valid?

marker_purple : this.site_base + "images/icon/marker-puple.png"  

EDIT: I can test it and it works fine btw.

ChruS
  • 3,707
  • 5
  • 29
  • 40
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Jon B Oct 25 '12 at 15:21
  • No, it isn't valid. Don't post wild guesses as answers when you can easily test them. – interjay Oct 25 '12 at 15:23