1

I have the following object.

ou.map : {
   settings : {
   },

   basePath : './path/';
   marker_setting : {
     img   :  this.basePath + 'image.png';
   },

   fun : function() {
      alert ( this.maker_setting.img  );
      //alert ( this.basePath + this.maker_setting.img ); Right now, I am using this where this.maker_setting.img is simply 'image.png'
   }

}

Here, value of ou.map.marker_setting.img comes out to be undefinedimage.png , but I want that to be ./path/image.png . How can I do that?

Edit
I am open to change the complete approach as well. All I want is, one namespace having marker_setting and basePath and one main method which can use property in marker_setting.

Also, I tried all these

img   :  this.basePath + 'image.png';
img   :  basePath + 'image.png';
img   :  ou.map.basePath + 'image.png';

None, of these gave desired output. Let me know if any other info is required.

Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
  • 4
    here `this` refers to the context where the statement gets executed. – Diode Mar 19 '13 at 07:32
  • have a look at [Javascript objects: get parent](http://stackoverflow.com/questions/2980763/javascript-objects-get-parent) –  Mar 19 '13 at 07:48
  • this answer uses an init function to initialise a parent http://stackoverflow.com/a/10170826/2022859 –  Mar 19 '13 at 07:58

1 Answers1

0

The value of this depends on how and where your above code gets executed, i.e. the context where ou.map object gets defined in your case.

For example:

function foo() {
   ou.map = {
      // here this would be 'x'
   }
}

foo.call('x'); // means the value of "this" inside foo() will be 'x'
techfoobar
  • 65,616
  • 14
  • 114
  • 135