Cannot get object name

let user = {
    invoke(){
        console.log(`${this}`);
    }
};

user.invoke();

result = [object Object]

Please someone tell me why it didn’t give name of the object

Try using console.log() without a template literal. Just use the this keyword, console.log(this).

2 Likes

What @austinlords says, but what are you expecting it to return? It is telling you that it’s an object. If you just return this, it will just give you

{ invoke: [Function: invoke] }

IE an object with a function called invoke on it: it doesn’t have a name, it’s just an object

1 Like

actually I wanted to return the name of the object

It’s just an object, it doesn’t have a name – what are you expecting it to log?

1 Like

ohh sorry my bad . actually I get confused. I thought that variable user is the name of the object and try to return the name.

1 Like

Remember, functions are objects in JavaScript!

1 Like