0
{
    title: "This is title of item 1",
    desc: "This is description of 1",
    state: "inactive",
    toggleState: () => {
        console.log('title: ', title)
    }
}

This is an object in Typescript. I want to access title variable from this object in this object's function toggleState(). But I get error as

[ts]: Cannot find name 'title'

Is it possible? if so, then how to achieve it?

Phil
  • 141,914
  • 21
  • 225
  • 223
Arnold Parge
  • 6,061
  • 1
  • 28
  • 30

1 Answers1

3

Does this work?

var obj = {
  title: "This is title of item 1",
  desc: "This is description of 1",
  state: "inactive",
  toggleState: function() {
    console.log('title: ', this.title)
  }
}.toggleState();
Satpal
  • 129,808
  • 12
  • 152
  • 166
Manuel Otto
  • 6,053
  • 1
  • 17
  • 24
  • 1
    Apologies for the previous comment. Didn't realised you changed from an arrow function to a regular one – Phil Aug 30 '17 at 06:30