0

Having an issue with regard to calling the function in typescript

I have a class

 export class Test{
     constructor(){}

     public func1(req:request,res:Response) {
 // call method
        let func2 =  this.Func2();

     }

     private Func2():string{
       return "Hello";
   }
}
export default new Test();

I have an express Router class

import {Router} from "express";
import Test from '../Handlers/Test';
export class UserRouter {

router: Router;

constructor() {
    this.router = Router();

}

Routes() {
    this.router.route('/user').post(Test.Func1);
    return this.router;
}

}

export default new UserRouter().Routes();

The issue is when I try to call the method Func2 using this.Func2() in Func1() method i get an error stating cannot call Func2 of undefined. I either have to call it in 2 ways

1) `new Test().Func2() inside Func1() method as

public Func1(req:request,res:Response){
   let func2 = new Test().Func2();
}

2) make Func2() as static and then call it using Test.Func2()

public Func1(req:request,res:Response){
   let func2 = Test.Func2();
}

private static Func2():string{
   return "Hello";
}

why can't I just call it using this.Func2() ?

Is it because I am exporting new of Test() to the router method and that object is no longer in memory?

4
  • If Func2 is static then it doesn't exist on this, only on Test. Commented Sep 14, 2017 at 11:59
  • @NitzanTomer that is correct, I can access it by making it static and calling it using Test.Func2 . My issue is that I am unable to call it using this.Func2() . I am able to also call it using new Test().Func2() Commented Sep 14, 2017 at 12:32
  • I can't say that I understand your problem. Can you please edit your question and add a clear example that reproduces your problem? Commented Sep 14, 2017 at 12:40
  • @NitzanTomer I have edited the question Commented Sep 14, 2017 at 12:54

2 Answers 2

1

1) new Test().Func2()

This will not work in Typescript level, because your method is private, so it is not accessible from the outside.

2) Test.Func2()

This will keep the function inside the Test itself, so the function will not been attached to any object ( this ).

What about your code?

Your code is working. Is this the same code you have tried?

 class Test{
     constructor(){}

     func1(req,res) {
        let func2 =  this.func2();
        console.log(func2);
     }

     func2(){
       return "Hello";
   }
}

const t = new Test();
t.func1();

Sign up to request clarification or add additional context in comments.

5 Comments

I am not trying to access Func 2 from outside. basically my Test class is imported into a router class. When a certain api call is made, I am routing it to Test.func1. In Func 1 i am trying to the private method of func 2
Your code is working. Error is another place of your code.
@Suren Srapyan - In above code sample, If I call func2 without this keyword. it will not work.What would be the reason?.
because this is a method, it is bound to the object So you are telling for each object I call it, it binds this to that object
@SurenSrapyan I have edited my question. I have included the routes file as well. Is there an issue in the way I am exporting my class to the router file?
0

Your problem is this:

this.router.route('/user').post(Test.Func1);

You are passing Test.Func1 to be invoked later as a callback, when that function is finally invoked the scope of this won't be the instance (Test) but something else, and so you have two options:

(1) Bind it:

this.router.route('/user').post(Test.Func1.bind(Test));

(2) Arrow function:

this.router.route('/user').post(() => Test.Func1());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.