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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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: 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.
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".
Thank you for your answer but Engineer.prototype.constructor = WorkerBee; and Engineer.prototype.constructor = new WorkerBee; are completly different as far as I understood
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.