简体   繁体   中英

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!

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

Try it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM