The lodash method `_.bind` exported as a module.
npx @tessl/cli install tessl/npm-lodash--bind@4.2.0The lodash method _.bind exported as a standalone module. This function creates a function that invokes func with the this binding of thisArg and prepends any additional arguments to those provided to the bound function.
npm install lodash.bindconst bind = require('lodash.bind');For ES modules:
import bind from 'lodash.bind';const bind = require('lodash.bind');
function greet(greeting, punctuation) {
return greeting + ' ' + this.user + punctuation;
}
const object = { user: 'fred' };
// Simple binding
const bound = bind(greet, object, 'hi');
bound('!');
// => 'hi fred!'
// With placeholder arguments
const bound2 = bind(greet, object, bind.placeholder, '!');
bound2('hello');
// => 'hello fred!'Creates a function that invokes the original function with a specific this context and optionally pre-applied arguments.
/**
* Creates a function that invokes `func` with the `this` binding of `thisArg`
* and prepends any additional `bind` arguments to those provided to the
* bound function.
*
* @param {Function} func The function to bind.
* @param {*} thisArg The `this` binding of `func`.
* @param {...*} [partials] The arguments to be partially applied.
* @returns {Function} Returns the new bound function.
*/
function bind(func, thisArg, ...partials);Usage Examples:
const bind = require('lodash.bind');
// Basic binding
function sayHello() {
return 'Hello, ' + this.name;
}
const person = { name: 'Alice' };
const boundSayHello = bind(sayHello, person);
boundSayHello(); // => 'Hello, Alice'
// Partial application
function add(a, b, c) {
return a + b + c;
}
const addTwo = bind(add, null, 2);
addTwo(3, 4); // => 9 (2 + 3 + 4)
// Placeholder usage
const addToFive = bind(add, null, bind.placeholder, 5);
addToFive(3, 2); // => 10 (3 + 5 + 2)The bind function supports placeholder arguments using bind.placeholder.
/**
* The placeholder value used for partially applied arguments.
* Defaults to the bind function itself.
*/
bind.placeholder;Usage Examples:
const bind = require('lodash.bind');
function multiply(a, b, c) {
return a * b * c;
}
// Skip the first argument using placeholder
const multiplyBy2And3 = bind(multiply, null, bind.placeholder, 2, 3);
multiplyBy2And3(4); // => 24 (4 * 2 * 3)
// Multiple placeholders
const customMultiply = bind(multiply, null, bind.placeholder, bind.placeholder, 5);
customMultiply(2, 3); // => 30 (2 * 3 * 5)Function.prototype.bindThe bind function will throw a TypeError if the first argument is not a function:
const bind = require('lodash.bind');
try {
bind('not a function', {});
} catch (error) {
console.log(error.message); // => "Expected a function"
}bind doesn't support placeholdersbind sets the length property, lodash bind doesn't/**
* The bind function type definition
*/
interface BindFunction {
<T extends Function>(func: T, thisArg: any, ...partials: any[]): T;
placeholder: any;
}