PDA

View Full Version : Extend multiple classes with the same methods



inf3rno
29 Dec 2010, 4:43 PM
Hi!

I want to extend Component,BoxComponent,Container and Panel with the same methods, how can I do that?


var A=function (){};
var B=function (){};

var m=
{
constructor: function ()
{
alert(arguments.callee.superclass.constructor==A);
}
};

var C=Ext.extend(A,m);
new C();

var D=Ext.extend(B,m);
new C();
Extjs overwrites here the constructor.superclass property after the second extend. Is there a built-in solution for this, or I have to use some tricks?

Condor
30 Dec 2010, 12:44 AM
This works for every method except the constructor. It has to be unique for every class you create.

inf3rno
30 Dec 2010, 3:17 AM
Ok, you're right!


var A=function (){};
var B=function (){};

var m=
{
test: function ()
{
alert(this.constructor.superclass.constructor==A);
}
};

var C=Ext.extend(A,m);
new C().test();

var D=Ext.extend(B,m);
new C().test();
new D().test();

inf3rno
30 Dec 2010, 3:50 AM
You can clone the constructor with this method:


Function.prototype.clone=function ()
{
if (!this.cloner)
{
this.cloner=new Function("return "+this.toString()+";");
}
return this.cloner();
};
This solution does not work in closure:


var a=1;
(function ()
{
var a=2;
var f=function ()
{
alert(a);
};
f.clone()(); // 1
})();

This will alert "1", because the clone is defined by "new Function" in the global namespace, not in the closure.