bind - mmedrano9438/peripheral-brain GitHub Wiki
bind() method of 'Function' instances creates a new function that, when called, calls this function with its 'this' keyword set to the provided value
const module = { x: 42, getX: function () { return this.x; }, };
const unboundGetX = module.getX; console.log(unboundGetX()); // The function gets invoked at the global scope // Expected output: undefined
const boundGetX = unboundGetX.bind(module); console.log(boundGetX()); // Expected output: 42
////////////////////////////another example
"use strict"; // prevent this
from being boxed into the wrapper object
function log(...args) { console.log(this, ...args); } const boundLog = log.bind("this value", 1, 2); const boundLog2 = boundLog.bind("new this value", 3, 4); boundLog2(5, 6); // "this value", 1, 2, 3, 4, 5, 6