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
  • 157,677
  • 23
  • 242
  • 245
Arnold Parge
  • 6,684
  • 2
  • 30
  • 34

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
  • 132,252
  • 13
  • 159
  • 168
Manuel Otto
  • 6,410
  • 1
  • 18
  • 25
  • 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