Ivar
Ivar

Reputation: 4392

JS: Access object variable inside sub-object

I guess this have been both asked and answered before, but I don't really know what to search for.

Anyway, lets say we've got the following JavaScript:

var foo = {
   myVar : 'Hello',
   bar : function() {
      //Fetch the myVar variable
   }
}

How do I access the variable where mentioned?

Thank you in advance!

Upvotes: 0

Views: 150

Answers (1)

Blacksad
Blacksad

Reputation: 15422

var foo = {
   myVar : 'Hello',
   bar : function() {
      alert(this.myVar);
   }
}

Try it

Upvotes: 2

Related Questions