Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COPlease don't do `Consultant.prototype = new Employee();`. This works as long as `Employee` does not expect any arguments. But what if it does? At this stage, you don't really want to invoke the constructor (you are doing this later inside the child constructor), you just want to get the prototype into the chain. This can be easily done with `Object.create`: `Consultant.prototype = Object.create(Employee.prototype)`.
      singulars
    2. CO`Consultant.prototype = new Employee();` is an important part of the prototypal inheritance pattern. It ensures that any time you do `new Consultant()` (with or without arguments), the new Consultant object will have all of the methods that the Employee object had when you did `new Employee()`. Note that a downside to the use of this pattern is that if the "base" constructor, Employee, does take arguments, it *must* be able to handle the case when no arguments are passed.
      singulars
    3. COMaybe I did not express myself clearly: To avoid this downside, you should use `Object.create` instead. To ensure that a new `Consultant` object is initialised properly, you call the `Employee` constructor inside the `Consultant` constructor (what you already). That's all. As I said, at that moment, you don't want to create an `Employee` instance you just want to get its prototype into the prototype chain. `Object.create` lets you do that without initialising a new `Employee` object.
      singulars
 

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