更好的方式来叫超类方法在过测试
-
19-09-2019 - |
题
所有的过测试文件和例子我已经阅读建议调用超类方法是这样的:
MyApp.MyPanel = Ext.extend(Ext.Panel, {
initComponent: function() {
// do something MyPanel specific here...
MyApp.MyPanel.superclass.initComponent.call(this);
}
});
我已经使用这种模式相当长的一段时间,主要的问题是,当你重新命名你的课后你还必须改变所有的话超方法。那是相当不方便,我常常会忘记,然后我们追踪奇怪的错误。
但是读取的来源 Ext.extend()
我发现,这不是我可以使用 superclass()
或 super()
方法, Ext.extend()
增加的原型:
MyApp.MyPanel = Ext.extend(Ext.Panel, {
initComponent: function() {
// do something MyPanel specific here...
this.superclass().initComponent.call(this);
}
});
在这个代码名MyPanel到别的东西很简单-我只需要更改一条线。
但是我怀疑...
我还没有看到这种记录的任何地方和老智慧说,我不应该依赖于非法的行为。
我没有找到一个使用这些
superclass()
和supr()
方法在过测试的源代码。为什么他们创建的时候你是不是打算使用它们?也许这些方法用在一些旧版本的过测试,但被否决,现在呢?但似乎这样一个有用的特征,为什么你会反对吗?
因此,我应该使用这些方法或没有?
解决方案
是的的确 supr()
不是记录在案。我一直期待着利用它在过测试为3.0.0(一个电话分机:工作人员回答说,在该论坛上,他们增加了它在这一版本),但似乎可怕的破坏。
它目前没遍继承层次结构,而是转达一个水平上,然后被卡在这一级上,循环无休止和打击了一叠(请参考).所以,如果你有两个或者更多 supr()
一排,你的应用程序将中断。我还没有找到任何有用的信息 supr()
在既无文档,也不论坛。
我不知道有关维护版本3.0.x因为我没有得到支持许可证...
其他提示
我认为这是解决过测试4callParent.
Ext.define('My.own.A', {
constructor: function(test) {
alert(test);
}
});
Ext.define('My.own.B', {
extend: 'My.own.A',
constructor: function(test) {
alert(test);
this.callParent([test + 1]);
}
});
这里有一个模式使用,并已经意义的博客了一段时间。
Ext.ns('MyApp.MyPanel');
MyApp.MyPanel = (function(){
var $this = Ext.extend(Ext.Panel, {
constructor: function() {
// Using a 'public static' value from $this
// (a reference to the constructor)
// and calling a 'private static' method
this.thing = $this.STATIC_PROP + privateStatic();
// Call super using $super that is defined after
// the call to Ext.extend
$super.constructor.apply(this, arguments);
},
initComponent: function() {
$super.initComponent.call(this);
this.addEvents([Events.SOMETHING]); // missing docs here
}
});
var $super = $this.superclass;
// This method can only be accessed from the class
// and has no access to 'this'
function privateStatic() {
return "Whatever";
}
/**
* This is a private non-static member
* It must be called like getThing.call(this);
*/
function getThing() {
return this.thing;
}
// You can create public static properties like this
// refer to Events directly from the inside
// but from the outside somebody could also use it as
// MyApp.MyPanel.Events.SOMETHING
var Events = $this.Events = {
SOMETHING: 'something'
}
return $this;
})();
MyApp.MyPanel.STATIC_STRING = 10;
//Later somewhere
var panel = new MyApp.Mypanel();
panel.on(MyApp.Mypanel.Events.SOMETHING, callback);
有很多的功能,你得使用这一模式,但你不需要使用他们的所有
你可以用这个鲜为人知的Javascript特征(参数。被叫):
MyApp.MyPanel = Ext.extend(Ext.Panel, {
constructor: function() {
// Do your thing
this.thing = 1;
// Call super
arguments.callee.superclass.constructor.apply(this, arguments);
}
});
看看 MDC的文件
编辑:实际上,这不会的工作与initComponent因为它并不构造。我总是替代的构造,个人(尽管是什么电话分机JS实例建议).将继续考虑这一点。
我只想改变你的代码:
var $cls = MyApp.MyPanel = Ext.extend(Ext.Panel, {
initComponent: function() {
// do something MyPanel specific here...
$cls.superclass.initComponent.call(this);
}
});
这样,你只保留一个单独的参考类的名称,现在$cls。只有使用$cls内的类方法和你会没事的。
我已经想出了这个解决方案几个小时前heheh...
function extend (parentObj, childObj) {
parentObj = parentObj || function () {};
var newObj = function () {
if (typeof this.initialize == 'function') {
this.initialize.apply(this, arguments);
}
}
newObj.prototype.__proto__ = parentObj.prototype;
for (var property in childObj) {
newObj.prototype[property] = childObj[property];
}
newObj.prototype.superclass = function (method) {
var callerMethod = arguments.callee.caller,
currentProto = this.constructor.prototype.__proto__;
while (callerMethod == currentProto[method]) {
currentProto = currentProto.__proto__;
}
return currentProto[method];
};
return newObj;
}
然后你可以这样做:
var A = function () {
this.name = "A Function!";
};
A.prototype.initialize = function () {
alert(this.name);
}
var B = extend(A, {
initialize: function () {
this.name = "B Function!";
this.superclass('initialize').apply(this);
}
});
var C = extend(B, {
initialize: function () {
this.superclass('initialize').apply(this);
}
});
测试只用(铬8.0.552.237(70801)Ubuntu10.10) 和(Firefox3.6.13).
希望这有助于个人时,我几乎被切换到应用程序.