-4

I have some question about function named bind.

I am testing some public source codes implemented in Angular. I am trying to understand what the bind function, I searched internet but can't find proper answer. Could you give some guide for this?

export class AppComponent {

  currentPage: number = 1;

  news: Array<any> = [];

  scrollCallback;

  constructor(private hackerNewsSerivce: HackerNewsService) {

    this.scrollCallback = this.getStories.bind(this);

   }

  getStories() {
    return this.hackerNewsSerivce.getLatestStories(this.currentPage).do(this.processData);
  }

  private processData = (news) => {
    this.currentPage++;
    this.news = this.news.concat(news.json());
  }

}
Anna Lee
  • 909
  • 1
  • 17
  • 37
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind – JB Nizet Mar 10 '18 at 14:17
  • 4
    I really wonder how you managed to not find the answer by searching the internet. What search query did you try? – JB Nizet Mar 10 '18 at 14:20
  • I tried with angular and bind, most of time angular binding relevant results are returned. I found some bind function in the javascript, but it was not interpreted to my case. so asked – Anna Lee Mar 10 '18 at 15:02

1 Answers1

0

In terms of angular, function binding is used to declare a function in component/directive and define in the scope / this.

And we use where we need like volatile

Satzz
  • 57
  • 2
  • 8
  • Actually, that code is for implementing infinite scrolling, now I understand a little bit about bind() function.. but... when I add that bind function exactly the way as same structure as the code above. it makes my web app blinking.. I added it in the constructor of the component where is responsible for searching into database and showing the result... could you guess what the problem might be? – Anna Lee Mar 10 '18 at 15:28