how bind works in javascript code example

Example 1: bind in javascript

bind() returns a bound function that, when executed later, will have the correct context ("this") for calling the original function.

Example 2: javascript bind

function myFunc(p1, p2, pN, addedParam)
{
     // here "this" equals "myThis"
}
let myThis = {};

// create new function that, when invoked, calls
// myFunc using myThis as context.
// prepend params as function arguments to those
// provided when invoked.
let f = myFunc.bind(myThis, "param1", "param2", "paramN");
f("addedParam");