简体   繁体   English

OO Javascript .prototype

[英]OO Javascript .prototype

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. 这来自Mozilla示例页面。 Why are there no parentheses for new new WorkerBee? 为什么新的WorkerBee没有括号? And if the WorkerBee constructor had arguments do we need to pass them in this line to? 并且,如果WorkerBee构造函数具有参数,是否需要在这一行中将其传递给?

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

I had this issue: JS Hint: Missing '()' invoking a constructor. 我遇到了这个问题:JS提示:缺少调用构造函数的'()'。

So I am wondering now, how the clean solution would look like. 所以我现在想知道,干净的解决方案会是什么样子。

Parentheses are optional when calling a constructor using the new keyword, although some linters will warn you. 使用new关键字调用构造函数时,括号是可选的,尽管有些短毛狗会警告您。 But it's entirely OK to use new WorkerBee instead of new WorkerBee() 但是使用new WorkerBee而不是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. 但是,作为程序员,选择完全取决于您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM