Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The variables <code>legs</code> and <code>iq</code> are simulating a "private" member to the <code>Human</code> class because they are visible only in that closure (only in the Human function).<br> If you want to access them from outside that scope, you need to <em>make them public</em> by binding them to the <code>this</code> keyword (<code>this.iq=100;</code>) or by implementing <strong>getters</strong> and <strong>setters</strong> for each of your <em>private</em> member : </p> <pre><code>function Human() { var legs = 2; this.getLegs = function(){ return legs; }; } </code></pre> <p>Anyway, these are represent just the very tip of the iceberg; I explained them so that you can understand why what you were trying to do failed.<br> If I understand correctly what you were trying to do, the right way to write your ideea oop in js would be something like this : </p> <pre><code>function Human(){} Human.prototype = { legs : 2, iq : 100 }; function Woman(){} Woman.prototype = new Human; Woman.prototype.getIq = function(){ return this.eq - 10; }; var womanInstance = new Woman(); // needless to say that this line is both wrong and misogynistic alert('a woman has an iq of ' + womanInstance.getIq()); </code></pre> <p>Sorry if I got carried away, but there are lots of articles &amp; blogs about javascript oop (such as <a href="https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript" rel="nofollow">this one</a>) that I recommend you read before/while getting stuck with these kind of problems.</p>
 

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