This is a migrated thread and some comments may be shown as answers.

Why use var that = this?

2 Answers 101 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ramesh
Top achievements
Rank 1
Ramesh asked on 14 Aug 2014, 07:41 PM
Pardon my ignorance but what is the reason behind the pattern of using 

var that = this;

in KenodUI? 

that is declared as a local variable so would it not always refer to this when a function on 
a widget is invoked? 

I must be missing something.

Thanks in Advance.

2 Answers, 1 is accepted

Sort by
0
Ramesh
Top achievements
Rank 1
answered on 15 Aug 2014, 12:25 PM
I think I figured it out. 

The method where we declare

var that = this;

itself could be a complext JS object (read function) with additional handlers. When such handlers are invoked, the context of "this" switches to whoever invokes the handler. However, we might still want to invoke other methods on the main container (for example, grid). This is where we can use "that" leveraging closures. 

Please correct if I am wrong.
Thx


0
Petyo
Telerik team
answered on 15 Aug 2014, 01:00 PM
Hi Ramesh,

you have understood this pattern usage correctly - the var that = this is used as a reference to the original context in closures (most often, event handlers). 

Regards,
Petyo
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
General Discussions
Asked by
Ramesh
Top achievements
Rank 1
Answers by
Ramesh
Top achievements
Rank 1
Petyo
Telerik team
Share this question
or