Why can’t we directly use this instead of self in the following example?
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
}
After responses, I’ve learned:
Yes, there is no need if there is no context switch in class.
But I will use this approach as “convention” although there is no need.
There’s no reason why you can’t use
thisdirectly there (and I would say it would be better for readability if you did).However, the
var self = this;is often needed in situations like the following (basically, any asynchronous action like event binding, AJAX handlers etc, where the resolution ofthisis deferred until it equals something else);