0
function Engineer (name, projs, mach) {
    this.base = WorkerBee;
    this.base(name, "engineering", projs);
    this.machine = mach || "";
}
Engineer.prototype = new WorkerBee;

var jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");

This is from a Mozilla example page. Why are there no parentheses for new new WorkerBee? And if the WorkerBee constructor had arguments do we need to pass them in this line to?

Engineer.prototype = new WorkerBee(BaseClassConstructorArgument1,2...)

I had this issue: JS Hint: Missing '()' invoking a constructor.

So I am wondering now, how the clean solution would look like.

Matthias
  • 1,386
  • 3
  • 24
  • 59

1 Answers1

3

Parentheses are optional when calling a constructor using the new keyword, although some linters will warn you. But it’s entirely OK to use new WorkerBee instead of new WorkerBee()

If you want to pass arguments, you naturally need the parentheses. That is why some people argue that it’s best to always use paratheses for consistency (often the same people that write lint rules).

But the choice is entirely up to you as a programmer.

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
  • I did not understand yet in which situations we would need to extend the prototype and in which not. Because the Engineer constructor will call the base class constructor anyway. – Matthias Dec 11 '12 at 09:20
  • @Matthias: I think you should use `call` to call the super class and pass the correct `this` context instead of just executing the function. You probably also need to fix the constructor `Engineer.prototype.constructor = WorkerBee;` to have a correct inheritance chain. – elclanrs Dec 11 '12 at 09:23
  • An answer to this would be useful, maybe another person is trying to understand the same example too. And it is related in terms of how and when we should use ".prototype". – Matthias Dec 11 '12 at 09:25
  • Thank you for your answer but Engineer.prototype.constructor = WorkerBee; and Engineer.prototype.constructor = new WorkerBee; are completly different as far as I understood – Matthias Dec 11 '12 at 09:27
  • I think you're mixing concepts. `Engineer.prototype = new WorkerBee` (not `constructor`) creates an inheritance chain but you still need to point to the parent constructor with `Engineer.prototype.constructor = WorkerBee`. – elclanrs Dec 11 '12 at 09:31
  • @Matthias The constructor calls the base class constructor, true, but when adding `Engineer.prototype = new WorkerBee;` you also "inherit" all WorkerBee prototypes into Engineer. If you want to understand how prototype chains work, maybe you should post a new question that focuses on this. – David Hellsing Dec 11 '12 at 09:31
  • @elclanrs for the record, pointing to the correct constructor does not have any effect on inheritance, it’s simply a convenience reference to the parent constructor (same as `this.base` in the example). – David Hellsing Dec 11 '12 at 09:33
  • Thank you. See my follow up question http://stackoverflow.com/questions/13819733/js-oo-pattern-with-prototype-and-base-class – Matthias Dec 11 '12 at 11:48