Harald Thomson
Harald Thomson

Reputation: 784

Typescript: Calling a "method" of another class

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 so far.

export class Foo {
   calcSomeThing(parameter:number): number {
      //stuff
   }
}

class Bar {
   var foo:Foo = new Foo();

   calcOtherThing() {
      result = foo.calcSomething(parameter)
   }
}

What is the correct way to call calcSomething on foo from calcOtherThing?


edit: added an instance of foo

Upvotes: 17

Views: 95067

Answers (5)

jmunsch
jmunsch

Reputation: 24089

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', '!')

Upvotes: 0

Joel Balmer
Joel Balmer

Reputation: 1479

It may not be right for all situations, but for the angular app I'm working on, I'm been using a service - here's what angular says about them. You can then call them like this:

smile.service.ts

export class SmileService {

  addSmileMethod(input: string): string {
    return input + ' :)';
  }

}

smile-component.ts

import { SmileService } from './path/to/smile.service';

export class SmileComponent {

  constructor(private smileService: SmileService) { }

  ngOnInit() {
    // Using the service
    const smileString = this.smileService.addSmileMethod('Hello!');
    console.log(smileString);

    // Output is:
    // Hello! :)
  }

}

Upvotes: 2

vzendara
vzendara

Reputation: 350

I believe you need a constructor for classes in TypeScript. In the example I provide I made mine data holders, but it's not required. Additionally, your calculation functions need to return values. Also, in order to use Foo in an instance of Bar, you need to make an instance of Foo.

class Foo {
   private data; 
   constructor(data: number) {
       this.data = data;
   }

   calcSomeThing(parameter:number): number {
      return parameter + 1;
   }
}

class Bar {
   private data;
   private foo:Foo = new Foo(3);

   constructor(data: number) {
       this.data = data;
   };

   calcOtherThing(): number {
      let result = this.foo.calcSomeThing(this.data);
      return result;     
   }
}

let bar = new Bar(5);
console.log(bar.calcOtherThing()); // returns 6

Upvotes: 3

Amid
Amid

Reputation: 22332

There are several problems with your code.

  1. Typescript is case sensitive. So "calcSomething" and "calcSomeThing" are two different methods.
  2. The only way to access cals methods and properties is through "this" keyword: this.foo
  3. To define class property use private/protected/public modifier. Or no modifier at all (that will be the same as public). So no things like "var foo" in the class body.

Taking this into account the fixed code would look like this:

export class Foo 
{
    calcSomeThing(parameter:number): number 
    {
        //Stuff
    }
}

class Bar 
{
    private foo:Foo = new Foo();

    calcOtherThing(parameter: number): number 
    {
            return this.foo.calcSomeThing(parameter)
    }
}

Upvotes: 22

dex
dex

Reputation: 137

calcSomeThing is a non-static method/function. Create an instance of Foo to be able to call it:

let foo:Foo = new Foo();
let result:number = foo.calcSomeThing( parameter );

Never use var in Typescript - let is your friend.

Upvotes: 9

Related Questions