我有3个对象。
netBuilder编号NumberingMethodDefault
我有一些扩展方法。
extend: function (Child, Parent) {
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.superclass = Parent.prototype;
this.mixin(Child, Parent);
},
/**
* @param {type} dst
* @param {type} src
* @returns {undefined}
*/
mixin: function (dst, src) {
var tobj = {};
for (var x in src) {
if ((typeof tobj[x] == "undefined") || (tobj[x] != src[x])) {
dst[x] = src[x];
}
}
if (document.all && !document.isOpera) {
var p = src.toString;
if (typeof p == "function" && p != dst.toString && p != tobj.toString &&
p != "\nfunction toString() {\n [native code]\n}\n") {
dst.toString = src.toString;
}
}
}然后我尝试从netBuilder和NumberingMethodDefault扩展到Numbering。
oopUtility.extend(Numbering, netBuilder);
oopUtility.extend(NumberingMethodDefault, Numbering);并调用超类
Numbering.superclass.constructor.call(this, arguments);
NumberingMethodDefault.superclass.constructor.call(this, arguments);编号具有方法setNumber()。我可以对netBuilder方法进行编号,但在NumberingMethodDefault中,我不能从编号执行setNumber()方法。
Uncaught TypeError: Object #<NumberingMethodDefault> has no method 'setNumber'NumberingMethodDefault的超类是什么?
console.log(NumberingMethodDefault.superclass);
//and it was netBuilder, not Numbering! о_О我怎么才能让它工作。我需要扩展3个或更多的对象!
发布于 2013-05-21 14:21:32
这是因为您对mixin的调用手动将属性从一个对象复制到另一个对象,并且会覆盖.superclass赋值。如果您正在模拟类继承,那么您将不需要以这种方式使用mixins,因此完全删除该代码应该不会有问题。
https://stackoverflow.com/questions/16662101
复制相似问题