My question is:
function Foo()
{
this.foo = "bar"; // <- What is "this" here?
}
From what I can tell it depends on how Foo is used, i.e. as a constructor or as a function. What can this be in different circumstances?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
thiskeyword refers to the object the function belongs to, or thewindowobject if the function belongs to no object.It’s used in OOP code, to refer to the class/object the function belongs to
For example:
This alerts:
Hello, worldYou can manipulate which object
thisrefers to by using theapply()orcall()functions. (A very very handy feature at times)