0

I researched this but only found articles (or threads) about getting the class name

For example:

const john = {
    firstName: this.constructor.name,
    lastName: 'smith'
}

console.log(john.firstName) // john

How can I access the object name as string so it outputs 'john' ?

sir-haver
  • 3,096
  • 7
  • 41
  • 85
  • 1
    You can't. The variable name "john" is not part of the object in any way; objects don't have names unless you add some sort of "name" property yourself. – Pointy Mar 01 '22 at 14:13
  • Interesting thank you very much – sir-haver Mar 01 '22 at 14:15
  • Thanks it's similar, not exactly what I was looking for but I see it's not possible as well probably for the same reason – sir-haver Mar 01 '22 at 14:20

0 Answers0