Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: Calling a "method" of another class

Tags:

typescript

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

like image 922
Harald Thomson Avatar asked Feb 22 '16 15:02

Harald Thomson


People also ask

How do you call a method from another class in TypeScript?

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.

Can you call methods from other classes?

To class a method of another class, we need to have the object of that class. Here, we have a class Student that has a method getName() . We access this method from the second class SimpleTesting by using the object of the Student class.

How do you expose the methods or properties from one class to another?

If it's a static method (doesn't use any instance data), then declare it as a static method and you can directly call it. If it's an instance method, then you would typically create an object of type one and then call the method on that object (usually in the constructor).

Can a method call another method JavaScript?

The JavaScript call() Method It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.


3 Answers

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)
    }
}
like image 152
Amid Avatar answered Oct 07 '22 16:10

Amid


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.

like image 26
dex Avatar answered Oct 07 '22 18:10

dex


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
like image 3
vzip Avatar answered Oct 07 '22 17:10

vzip