Sergiti Sergiti - 6 months ago 32
Javascript Question

What is the role of the this variable when it comes to function currying using the .bind()?

I came across to the following JS code (ES5)
and I don't really I understand what is the meaning of the this variable.

function multiply(a,b){
return a * b;

var multipleByThree = multiply.bind(this,3);

multipleByThree(10) // outputs 30

I do understand that the bind copies the multiply function and that 'a' parameter of it, will have the value 3. But what is the purpose of the this variable?

Can you help me out please?

Answer Source

The this variable that you are providing to .bind() is the context. In your case, this refers to the global object space.

Here's an example of how this works:

var message = 'within global context';
function multiply(a,b){
  return a * b;

var someOtherContext = {
  message: 'within some other context'

var multipleByThree = multiply.bind(this,3);
var multipleByThreeOtherContext = multiply.bind(someOtherContext, 3);


By changing the context that multiply executed within, we can change what variables it references.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download