最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create link by reference to method in sub class - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a comment  | 

3 Answers 3

Reset to default 0

You 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();
    }
});
发布评论

评论列表(0)

  1. 暂无评论