The Airbnb JavaScript Style Guide recommends using "_this" when saving a reference to the object, as seen in their Style Guide.
// bad
function() {
var self = this;
return function() {
console.log(self);
};
}
// bad
function() {
var that = this;
return function() {
console.log(that);
};
}
// good
function() {
var _this = this;
return function() {
console.log(_this);
};
}
However, some books, like AngularJS: Up and Running, argue that using "self" is preferable.
<script type="text/javascript>
angular.module('notesApp', []).controller('MainCtrl', [function () {
var self = this;
self.message = 'Hello ';
self.changeMessage = function () {
self.message = 'Goodbye';
};
}]);
</script>
So, the question remains - what is the reason for using "_this" over "self"?