An example of what that means in javascript


You might find someone else writing code that says: var that = this; What does that mean?

In javascript, this represents the current object.

Var that=this simply copies the current this object into that variable. What’s the point?


$('#zhetenga').click(function(){
//This is the clicked #zhetenga
var that = this;
$('.zhetenga').each(function(){
//This is the current object in the.zhetenga loop
//That is still the #zhetenga that was just clicked
});
});

As you can see, this object can change at any time in the program, and after var that=this, it still points to the current this before that is changed, so that the original object cannot be found.