I see many people using the "this" keyword in an object oriented context in JavaScript. Similar to how "self" is used in Python and some other languages. In an object oriented context, I have been able to avoid using "this" by using the "module pattern".
For example:
var robot = (function() {
// private variables
var status = "on";
var name;
// private function
function turnOff() {
status = "off";
}
// public function
function setName(new_name) {
name = new_name;
}
return {
setName: setName
};
})();
Or, this pattern:
var robot = function(robot_name) {
// private variables
var status = "on";
var name = robot_name;
// private function
function turnOff() {
status = "off";
}
// public function
function setName(new_name) {
name = new_name;
}
return {
setName: setName
};
};
var FooBot = new robot('Foo');
var BarBot = new robot('Bar');
Is using "this" just a preference? Or, am I missing out on something?