How do I use ‘this” keyword in an object literal? [closed]

You aren’t assigning a function and an object to the same variable you are redefining the what the symbol foo is bound to. Everytime you set foo = you are binding what is on the right hand side to that symbol. You can’t use this to access the function because it is a different object. If you want an object that has a function on it you have to define it like this

var foo = {
     myVal: 123,
     myFunction: function(){ return this.myVal;}
   }

Leave a Comment