I saw in many source codes:
var me = this;
specially in Ext-JS 4 (JS framework). Why doing such thing? Is there any other reason or you just want for a variable to be called like “me” instead of “this”?
Thank you.
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.
Usually so you can keep a reference to
thisinside a scope in whichthisrefers to something else (like a callback function, for example).Consider this example, in which the click event handler function has a different context to what you may expect (
thisdoesn’t refer to an instance ofMyClass):Now consider this example, in which we store a reference to the value of
thisinside the constructor function, and use that inside the callback function:The callback function can refer to a variable that was declared in the outer function, even after that function has returned (the
MyClassconstructor returns as soon as it’s executed theaddEventListener). This is a demonstration of a closure.