我正在阅读 Mozilla 的 Javascript指南当他们将 JS 与 Java 进行对比时,这让我想到,Java 代码很容易与他自己文件中的每个类分开。在进一步搜索之后,我了解到在具有命名空间和模块模式的 JS 中也可以实现相同的功能 - 我弄乱了它但很困惑(尤其是在将 File1.js 中声明的构造函数调用到 File2.js 时)
所以这是层次结构:
但我就是不知道如何让它正常工作
我如何简单地从
//employe.js
function Employee () {
this.name = "";
this.dept = "general";
}
function Manager () {
this.reports = [];
}
Manager.prototype = new Employee;
function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee;
function SalesPerson () {
this.dept = "sales";
this.quota = 100;
}
SalesPerson.prototype = new WorkerBee;
对此:
// employe.js
function Employee () {
this.name = "";
this.dept = "general";
}
// Manager.js
function Manager () {
this.reports = [];
}
Manager.prototype = new Employee;
// WorkerBee.js
function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee;
// SalesPerson.js
function SalesPerson () {
this.dept = "sales";
this.quota = 100;
}
SalesPerson.prototype = new WorkerBee;