在 javascript 中我想实现以下目标:
A 类(数组)-> B 类(实体容器)-> C 类(实体列表)
在代码中:
function EntityContainer() {
...
}
EntityContainer.prototype = new Array;
EntityContainer.prototype.constructor = EntityContainer;
EntityContainer.prototype.parent = Array.prototype
function EntityList() {
...
}
EntityList.prototype = new EntityContainer;
EntityList.prototype.constructor = EntityList;
EntityList.prototype.parent = EntityContainer.prototype
当然,继承的东西可以封装在“inheritsFrom”函数中。
这是实现我在迭代所有元素时可以在 EntityList 中调用“this[i]”(使用 this.length)的正确方法吗?
这里还有什么可以做得更好的吗?
谢谢大家!
最佳答案
您想要执行您所描述的操作的唯一原因是使用 instanceof
运算符进行类型检查。如果您不需要它,那么最好使用组合和数组对象作为实例变量。 []
访问器可以忽略不计,可以用 get()
替换。
关于Javascript多层继承: A->B->C,在C中使用A中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11154168/