The modern build of lodash's internal `bindCallback` as a module
npx @tessl/cli install tessl/npm-lodash--bindcallback@3.0.0lodash._bindcallback is a specialized callback binding utility that provides optimized function binding with this context and argument count specifications. It's part of the Lodash utility library ecosystem, exported as a standalone Node.js module for efficient callback management.
npm install lodash._bindcallbackvar bindCallback = require('lodash._bindcallback');var bindCallback = require('lodash._bindcallback');
// Basic function binding with context
function greet(name) {
return this.prefix + ' ' + name;
}
var context = { prefix: 'Hello' };
var boundGreet = bindCallback(greet, context);
console.log(boundGreet('World')); // "Hello World"
// Optimized binding with argument count
function processItem(value, index, collection) {
return this.transform(value) + index;
}
var processor = { transform: function(v) { return v.toUpperCase(); } };
var boundProcessor = bindCallback(processItem, processor, 3);
console.log(boundProcessor('hello', 0, ['hello'])); // "HELLO0"Creates a bound version of a function with specified this context and optional argument count optimization.
/**
* A specialized version of baseCallback which only supports this binding
* and specifying the number of arguments to provide to func.
*
* @param {Function} func The function to bind.
* @param {*} thisArg The this binding of func.
* @param {number} [argCount] The number of arguments to provide to func.
* @returns {Function} Returns the callback.
*/
function bindCallback(func, thisArg, argCount);Parameters:
func (Function): The function to bind. If not a function, returns identity function.thisArg (*): The this binding context for the function. If undefined, returns original function.argCount (number, optional): Number of arguments to optimize for. Supports optimized implementations for 1, 3, 4, and 5 arguments.Returns:
Behavior:
func is not a functionthisArg is undefinedapply pattern for other argument countsUsage Examples:
var bindCallback = require('lodash._bindcallback');
// Example 1: Basic binding without argument count
function multiply(a, b) {
return this.factor * a * b;
}
var math = { factor: 2 };
var boundMultiply = bindCallback(multiply, math);
console.log(boundMultiply(3, 4)); // 24
// Example 2: Optimized for single argument (argCount: 1)
function transform(value) {
return this.prefix + value + this.suffix;
}
var formatter = { prefix: '[', suffix: ']' };
var boundTransform = bindCallback(transform, formatter, 1);
console.log(boundTransform('test')); // "[test]"
// Example 3: Optimized for collection iteration (argCount: 3)
function processElement(value, index, collection) {
return this.base + value + index;
}
var processor = { base: 'item-' };
var boundProcess = bindCallback(processElement, processor, 3);
// Optimized for array map/forEach operations
// Example 4: Optimized for reduce operations (argCount: 4)
function accumulate(acc, value, index, collection) {
return acc + this.weight * value;
}
var reducer = { weight: 0.5 };
var boundAccumulate = bindCallback(accumulate, reducer, 4);
// Optimized for array reduce operations
// Example 5: Edge case - non-function input
var notAFunction = "not a function";
var result = bindCallback(notAFunction, {}); // Returns identity function
// Example 6: Edge case - undefined thisArg
var originalFunc = function() { return 'test'; };
var unchanged = bindCallback(originalFunc, undefined); // Returns originalFuncThe function provides specialized implementations for common argument patterns:
(value, index, collection)(accumulator, value, index, collection)(value, other, key, object, source)For other argument counts, it falls back to the general Function.prototype.apply approach.
This is an internal Lodash utility exposed as a standalone module. It's designed for:
The module is part of Lodash 3.x's modular architecture, allowing selective importing of specific utilities without the full Lodash library.