I'm trying to grasp the module pattern with added inheritance. I come from a university background, with mostly Java in my trunk, but I have been working with web techniques for about ten years. I'm only about a year into JavaScript though...
Anyway, I'm trying a simple inheritance (.prototype
) example. From a People
object, you can add Gentleman
's and then list them using their .toString()
method. A Gentleman
is a child to a Human
. It went good until I implemented the "abstract" Human
, but now the code will not run.
Please, comment on what is considered bad with my code. I would like to stay with the module/prototype approach though, but what am I doing wrong? I would also listen to what this
means in different contexts. I.e., in People
I can use the private _people
directly, but in submodules I have to use this._name
--why?
var People = People || {};
People = (function() {
var People = function(){
this._people = [];
};
var addGentleman = function (name) {
this._people.push(new People.Gentleman(name));
};
var getList = function () {
var temp = [];
for (var i = 0; i < this._people.length; i++) {
temp.push(this._people[i].toString());
}
return temp;
};
People.prototype.constructor = People;
People.prototype.addGentleman = addGentleman;
People.prototype.getList = getList;
return People;
})();
People.Human = (function () {
var Human = function (name, hasLadyParts) {
this._name = name;
this._hasLadyParts = hasLadyParts;
};
var hasLadyParts = function () {
return this._hasLadyParts;
};
var toString = function () {
var str = this._name;
if (!this._hasLadyParts) str += ' no';
return str + ' lady parts.';
};
Human.prototype.constructor = Human;
Human.prototype.hasLadyParts = hasLadyParts;
Human.prototype.toString = toString;
return Human;
})();
People.Gentleman = (function () {
var Gentleman = function (name) {
People.Human.call(this, name, false);
}
var toString = function () {
return 'Mr.' + People.Human.toString();
};
// Gentleman.prototype = Object.create(People.Human.prototype);
Gentleman.prototype.constructor = Gentleman;
Gentleman.prototype.toString = toString;
return Gentleman;
})();
$(function () {
var people = new People();
people.addGentleman('Viktor');
people.addGentleman('Joakim');
var list = people.getList();
var $ul = $('#people');
for (var i = 0; i < list.length; i++) {
$ul.append('<li>' + list[i] + '</li>');
}
});
Fiddle: http://jsfiddle.net/5CmMd/5/
Edit: I've updated code and fiddle a bit. If I get this working, I think I understand most of the design. This example would also work as a simple tutorial for future OOP programmers visiting JavaScript land, I think.