Note that there are some explanatory texts on larger screens.

plurals
  1. POGrasping "OOP" JavaScript: module pattern with inheritance
    primarykey
    data
    text
    <p>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...</p> <p>Anyway, I'm trying a simple inheritance (<code>.prototype</code>) example. From a <code>People</code> object, you can add <code>Gentleman</code>'s and then list them using their <code>.toString()</code> method. A <code>Gentleman</code> is a child to a <code>Human</code>. It went good until I implemented the "abstract" <code>Human</code>, but now the code will not run.</p> <p>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 <code>this</code> means in different contexts. I.e., in <code>People</code> I can use the private <code>_people</code> directly, but in submodules I have to use <code>this._name</code>--why?</p> <pre><code>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 &lt; 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 &lt; list.length; i++) { $ul.append('&lt;li&gt;' + list[i] + '&lt;/li&gt;'); } }); </code></pre> <p>Fiddle: <a href="http://jsfiddle.net/5CmMd/5/" rel="nofollow">http://jsfiddle.net/5CmMd/5/</a></p> <p><strong>Edit:</strong> 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload