Is there any method to set link to sub class function by reference?
For example:
main program -> create object of classA which inside create object of classB
inside classB contain function-B
From main program if I want to call function-B I will have to use ClassA.ClassB.function-B()
If I want to call classA.function-B() , I will have to create function-B in classA which calls classB.function-B();
If I want to change function-B, I will have to change classB, then ClassA then main program.
I am looking for methods to create function-B in classA which by reference to classB function-B().
This way when I only need change function-B in classB and main program without need change classB.
Is there something like this? in classA function-B -> classB.function-B() ;
Is there any method to set link to sub class function by reference?
For example:
main program -> create object of classA which inside create object of classB
inside classB contain function-B
From main program if I want to call function-B I will have to use ClassA.ClassB.function-B()
If I want to call classA.function-B() , I will have to create function-B in classA which calls classB.function-B();
If I want to change function-B, I will have to change classB, then ClassA then main program.
I am looking for methods to create function-B in classA which by reference to classB function-B().
This way when I only need change function-B in classB and main program without need change classB.
Is there something like this? in classA function-B -> classB.function-B() ;
Share Improve this question edited 2 days ago mplungjan 178k28 gold badges181 silver badges240 bronze badges asked 2 days ago Charles CandyCharles Candy 275 bronze badges3 Answers
Reset to default 0You can use a proxy to call B
dynamically:
class B{
method(){
console.log(this.constructor.name + '#method()');
}
}
class A{
constructor(){
const b = new B;
return proxyMethods(this, b);
}
}
new A().method();
function proxyMethods(a, b){
return new Proxy(a, {
get(self, prop){
if(prop in self){
return Reflect.get(...arguments);
}
if(typeof b[prop] === 'function'){
return b[prop].bind(b);
}
}
});
}
You can simplify it this way:
class B {
method() {
console.log( this.constructor.name + '#method()' );
}
}
class A {
constructor() {
this.b = new B;
}
method() {
console.log( this.constructor.name + '#method()' );
// invoke "b" method
this.b.method();
}
}
new A().method();
Unfortunatly your decription is not very specific on how this is ExtJS related. I am trying to nail your question here:
In ExtJS there are several ways to do so:
A) Util class: allows to be called from eveywhere
Ext.define('MyApp.util.Helper', {
singleton: true,
alternateClassName: 'UtilHelper',
methodX: function() {}
});
Ext.define('MyApp.view.MainViewController', {
extend: 'Ext.app.ViewController',
alias: 'controller.main',
init: function() {
UtilHelper.methodX();
}
});
B) Mixin: Adds all functions from the mixin to the class itself, if not already in the class.
Ext.define('MyApp.mixins.Helper', {
extend: 'Ext.Mixins',
methodX: function() {}
});
Ext.define('MyApp.view.MainViewController', {
extend: 'Ext.app.ViewController',
alias: 'controller.main',
mixins: ['MyApp.mixins.Helper'],
init: function() {
this.methodX();
}
});
C) Extend: Extend from another class instead of some Ext functionality.
Ext.define('MyApp.view.BaseController', {
extend: 'Ext.app.ViewController',
methodX: function() {}
});
Ext.define('MyApp.view.MainViewController', {
extend: 'MyApp.view.BaseController',
alias: 'controller.main',
mixins: ['MyApp.mixins.Helper'],
init: function() {
this.methodX();
}
});