JS Methods - Currying, call, apply and bind

by Anchit Gupta

JavaScript

//currying in js
let multiply = function(x){ // this is a closure function in js
	return function(y){
  	console.log(x*y);
  }
}
let multiplyByTwo = multiply(2);
multiplyByTwo(3);

// call, apply, bind

let name  = {
	firstName: "Anchit",
  lastName: "Gupta"
};

let printMyFullName = function(town, state){
	console.log(this.firstName + " " + this.lastName + " ," + town + " , " + state);
}

let name2  = {
	firstName: "Ankit",
  lastName: "Bansal"
};

//call method
printMyFullName.call(name, "Agra", "UP");

//apply method
printMyFullName.apply(name2, ["Noida", "UP"]);

//bind method
let printMyName = printMyFullName.bind(name, "Mumbai", "Maharashtra");
printMyName();

// bind method just attach the this to the function and returns the copy of that function.