let user = {
invoke(){
console.log(`${this}`);
}
};
user.invoke();
result = [object Object]
Please someone tell me why it didn’t give name of the object
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).
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
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?
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.
Remember, functions are objects in JavaScript!