2

Im quite new to javascript and sometimes I find it really hard to find what object a keyword 'this' refers to. Is there a code editor that is able to do that for me? Or any other advice that could help me :) Thanks!

2
  • Paste some code you're having trouble with. That will make it easier for people to help you. – pythonpython Dec 11 '20 at 15:02
  • I think your are looking for alert(this.constructor.name); – R4444 May 12 at 4:44
1

There's a great resource on W3Schools.

In summary:

It (this) has different values depending on where it is used:

  • In a method, this refers to the owner object.

  • Alone, this refers to the global object.

  • In a function, this refers to the global object.

  • In a function, in strict mode, this is undefined.

  • In an event, this refers to the element that received the event.

  • Methods like call(), and apply() can refer this to any object.

Answered with regard to getting function name here and with regard to getting function body here

2
  • Hi, thanks for your answer, but I know how it is used and what i'm looking for is an automatic way to reveal the corresponding object in a long and hard (obfuscated) code. Sorry if I wasn't clear. – Hukei Dec 12 '20 at 18:37
  • It's evaluated at runtime, so looking at the code won't tell you anything. The code needs to be executed to understand what this means. Functions are mutable in javascript and can be defined (created) at runtime. You can have a function which creates functions through lambdas. To say ' I wan't to know what this means in a program ' is to say ' I want to know how the program is evaluated up to the point where this line of code is called.' The only way to know that is to evaluate the code. – pythonpython Dec 13 '20 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.