Function

Function

Source:

Methods

bind(thisArg, …argumentsopt) → {function}

Source:

Creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. Does not work with new funcA.bind(thisArg, args). MDN

Example
// Bind Scope
var module = {
  x: 42,
  getX: function () { return this.x; }
};

console.log(module.getX()); // Outputs: 42

var unboundGetX = module.getX;
console.log(unboundGetX()); // Outputs: undefined
// Because the function gets invoked at the global scope.

var boundGetX = unboundGetX.bind(module);
console.log(boundGetX()); // Outputs: 42

// Bind Argument
var addArguments = function (arg1, arg2) { return arg1 + arg2; };

addArguments(1, 2); // Returns: 3

var addThirtySeven = addArguments.bind(null, 37);
addThirtySeven(); // Returns: NaN. Because 37 + undefined
addThirtySeven(5); // Returns: 42. Because 37 + 5 = 42
addThirtySeven(5, 10); // Returns: 42. Because 10 is ingnored.
Parameters:
Name Type Attributes Description
thisArg *

The value to be passed as the this parameter to the target function

arguments * <optional>
<repeatable>

Arguments to prepend to arguments provided to the bound function

Returns:
  • A copy of the given function with the specified this value and initial arguments
Type
function