Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

HTML5 .bind(this) what?

New Here ,
Mar 24, 2020 Mar 24, 2020

I am trying to wrap my head around the HTML5 alternate code. I am trying to wrap my head around the .bind(this);

this.button_1.addEventListener("click", fl_ClickToGoToAndPlayFromFrame.bind(this));

I understand 'this' for the start and it is reffereing to this instance ( I think). But then the call to teh function adds .bind(this). What is the purpose of this when simply calling a function? I tried to revove it, but that casued it to no longer work.

 

Also, is the 'fl_' important to have in the function name?

 

Thanks. 

532
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2020 Mar 24, 2020

Bind forces a function to execute in the desired context, which means the value of "this". Without it, all event handlers execute in the global context, which means "this" points to the browser window object. This is only a problem if the event handler uses "this". Apparently yours does.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2020 Mar 24, 2020
LATEST

Thank you for explaning it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines