Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This code:</p> <pre><code>function Consultant() { Employee.call(this); } </code></pre> <p>Is invoking the Employee constructor when the Consultant constructor is invoked (i.e., when an instance of Consultant is created). If the Employee constructor were doing any sort of initialization, then it would be important that it be called when the Consultant "subtype" was created.</p> <p>This code:</p> <pre><code>Consultant.prototype.constructor = Consultant.create; </code></pre> <p>is a bit of mystery. It implies that there is a function named create which is a property of the Consultant function object. However, in the code sample you posted, there is no such property. In effect, this line is assigning <code>undefined</code> to the Consultant constructor.</p> <p>Your question doesn't ask, but just FYI, I think what you probably want <em>instead</em> of that line with the create function, is this:</p> <pre><code>Consultant.prototype = new Employee(); Consultant.prototype.constructor = Consultant; </code></pre> <p>That's the prototypal inheritance pattern. It is certainly not the only or necessarily best approach, but I like it.</p> <p><strong>Update</strong></p> <p>If Employee takes an argument, you can handle that like so:</p> <pre><code>// Employee constructor function Employee(name) { // Note, name might be undefined. Don't assume otherwise. this.name = name; } // Consultant constructor function Consultant(name) { Employee.call(this, name); } // Consultant inherits all of the Employee object's methods. Consultant.prototype = new Employee(); Consultant.prototype.constructor = Consultant; </code></pre>
 

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