Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Understanding the bind method. Reference vs value

I don’t know if this is a dumb question, but i will ask it anyways.

I saw in a javascript course that we can use the bind method on functions to create basically the same functions but more specific. So like this for example:

const addTax = (rate, value) => value + value * rate;

                           null because we don't need this 
const addVAT = addTax.bind(null, 0.23); 

So basically what we’re doing here is that we create a new function called addVAT based on the addTax function but the difference is that we set the rate hardcoded at 0.23.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now to my question: Since functions are objects and objects are passed by reference, shouldn’t the addTax function become now the addVAT function because they both point to the same reference, just like here?:

const person = {
  name: 'test',
};
const person1 = person;
person1.age = 20;

Because when we initalize person1 to person and when we add the property age to person1 it also adds to person

>Solution :

No.


First you create a function and assign it to addTax

const addTax = (rate, value) => value + value * rate;

Then you call the bind method of that function and assign its return value to addVAT.

const addVAT = addTax.bind(null, 0.23); 

The purpose of the bind method is to create a new function and return it.

It doesn’t just return the original function (which would be pointless).

The value of addVAT is the new function created by bind.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading