Skip to content
Advertisement

How to bind an object to a function in JavaScript?

I have the next task:

JavaScript

I have to write a function that prints: "Mr Arnold Helgov"

My realization of this task is the next function:

JavaScript

But I have a problem here. My function prints only "Mr Helgov" and the name 'Arnold' is lost. How to solve this problem (maybe by using bind or apply or call)?

Advertisement

Answer

How to solve this problem (maybe by using bind or apply or call)?

Yes, you can use call or bind to make sure this gets to be equal to person during the call of fn.

With call:

JavaScript

Or, bind:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement