There is a javascript object

screen = { title: thisObjectName } 

You need to assign the variable thisObjectName value equal to the name of the object in this case, this value is equal to screen

  • 3
    It's impossible. Screen is a link to an object, the object itself knows nothing about it and cannot know it. If I write x = screen , I will have two absolutely identical links . What name do you want to know "x" or "screen"? - Alexey Ten
  • one
    I think you need to reconsider the approach to the implementation of your task - Rostyslav Kuzmovych

1 answer 1

If you need to know the name of the object to which you are directly accessing (and for this, a crutch with a title so maybe you need to create an object through the constructor function, and even better through the class? Then you will have access to the name of this class

 class Screen { constructor() { this.foo = 'bar' } } const inst = new Screen() console.log(inst.foo) //bar console.log(inst.constructor.name) //Screen 

if you really need so much to know not the name of the class but the name of the variable that contains the pointer to the object, then you can inject this name through the constructor

 class Screen { constructor(name) { this.title = name } } const screen = new Screen('screen') console.log(screen.title) //screen 
but better write why do you need it all. I think there are standard solutions to your problem.