0

Scenario 1:

below method is from services.ts:

onChange(){
    if(this.initcount == null){
        this.likescount = null;
    } else {
      this.likescount = this.initcount;
    } 
}

when using it in component.ts:

  constructor(service: LikesService) {  
    this.getchange = service.onChange; ------------------> without '()'?
  }

Scenario 2: below method is from services.ts:

getArray(){
            return this.courseArray;
}

when using it in component:

constructor(service: CoursesService) {
      this.courses = service.getArray();  --------------------> with '()' ???
  }
  • 1
    Actually, should be dupe of [In JavaScript, does it make a difference if I call a function with parentheses?](https://stackoverflow.com/q/3246928/215552). TypeScript is a superset of JavaScript and does not differ in this respect. – Heretic Monkey Jan 08 '20 at 22:43
  • 2
    If you aren't using parentheses, you aren't "calling" the method. – Sean Bright Jan 08 '20 at 22:43

1 Answers1

1
this.getchange = service.onChange;

☝️ This isn't calling onChange. It is setting getchange to be the onChange function. Somewhere else in the code, you can then use getchange to indirectly execute the code in onChange by doing getchange ().

V Maharajh
  • 9,013
  • 5
  • 30
  • 31