Why can\'t we directly use this
instead of self
in the following example?
function SeatReservation(name, initialMeal) {
var self =
Based on your example, there is "no" reason for doing this.
There is however, situations where it will help you, although some may frown upon it's usage.
i.e.
$('a.go').click(function(e)
{
e.preventDefault();
if(!$(this).hasClass('busy'))
{
$(this).addClass('busy');
$.ajax(
{
success : function(resp)
{
$(this).removeClass('busy');
},
error : function()
{
$(this).removeClass('busy');
}
});
}
});
In the above, $(this)
within the success and error callbacks would not reflect to the link you clicked, as the scope has been lost.
To get around this, you would do var self = $(this)
i.e.
$('a.go').click(function(e)
{
e.preventDefault();
if(!$(this).hasClass('busy'))
{
$(this).addClass('busy');
var btn = $(this);
$.ajax(
{
success : function(resp)
{
btn.removeClass('busy');
},
error : function()
{
btn.removeClass('busy');
}
});
}
});