Typescript: Calling a “method” of another class

后端 未结 5 2068
故里飘歌
故里飘歌 2021-02-13 11:51

I\'m pretty new to java-/type-script and I\'ve some troubles grasping their concepts. I would like to call a method of another class. However, I\'ve been unsuccessful s

5条回答
  •  你的背包
    2021-02-13 12:24

    Here's another example, but with a shared exported method.

    a.ts:

    export function sharedMethod(a, b, c) { return a + b + c }
    
    export default class A {
       constructor(a, b, c) {
           this.concat = sharedMethod(a,b,c);
       };
    }
    

    And in b.ts:

    import { sharedMethod } from './a'
    
    export default class B {
       constructor(a, b, c) {
           this.concat = sharedMethod(a,b,c)
       };
    }
    

    and in c.ts:

    import './a'
    import './b'
    new A('hello', 'world', '!')
    new B('hello', 'world', '!')
    

提交回复
热议问题