I saw a style that use
var test = function() {
var that = this;
this.show() {
that.***;
}
}
I am wondering why use that in the function?
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 purpose of using
thatis to capturethisin the context in which the function was constructed. When the function is called,thisis in a different context (The caller I believe) so that when test() is called,thiswould not be what you expected it to be (unless you understand JavaScript, in which case it would be what you expect it to be, but not what you want it to be).