0

I have below code trying to display the context from where checkObj() is invoked. For example, I need to see the output as 'a' since checkObj is invoked as a.checkObj(). Currently, only the type of 'this' is displayed i.e., Object{}

function checkObj(){
console.log(this);
}

var a = {checkObj:checkObj};
a.checkObj();
10
  • 1
    Objects don't have names; variables do. Objects are not necessarily stored in variables (or just one variable, for that matter). Commented May 9, 2018 at 6:47
  • If you want to log the variable name, a? That won't be possible. Commented May 9, 2018 at 6:47
  • Possible duplicate of Getting the object variable name in JavaScript Commented May 9, 2018 at 6:51
  • @melpomene So, the only way to find out where the checkObj() is invoked from is by referring the call-stack through debugger tool in browser? Commented May 9, 2018 at 6:53
  • 1
    If you already have a reference to what you are checking against, you can do myObj === this. If you explain what you are actually trying to do and why, may be there is a different way of doing it. Commented May 9, 2018 at 7:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.