or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--bindcallback

The modern build of lodash's internal `bindCallback` as a module

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash._bindcallback@3.0.x

To install, run

npx @tessl/cli install tessl/npm-lodash--bindcallback@3.0.0

index.mddocs/

lodash._bindcallback

lodash._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.

Package Information

  • Package Name: lodash._bindcallback
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash._bindcallback

Core Imports

var bindCallback = require('lodash._bindcallback');

Basic Usage

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"

Capabilities

Callback Binding

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:

  • (Function): A bound callback function optimized for the specified argument count.

Behavior:

  • Returns identity function if func is not a function
  • Returns original function if thisArg is undefined
  • Provides optimized implementations for argument counts 1, 3, 4, and 5
  • Falls back to general apply pattern for other argument counts

Usage 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 originalFunc

Performance Optimizations

The function provides specialized implementations for common argument patterns:

  • 1 argument: Optimized for simple transformations and filters
  • 3 arguments: Optimized for array iteration methods (map, forEach) with (value, index, collection)
  • 4 arguments: Optimized for reduce operations with (accumulator, value, index, collection)
  • 5 arguments: Optimized for comparison operations with (value, other, key, object, source)

For other argument counts, it falls back to the general Function.prototype.apply approach.

Error Handling

  • Invalid function input: Returns identity function instead of throwing
  • Undefined context: Returns original function unchanged
  • Missing arguments: Handles gracefully with default behavior

Implementation Notes

This is an internal Lodash utility exposed as a standalone module. It's designed for:

  • High-performance callback binding scenarios
  • Functional programming patterns requiring context binding
  • Integration with other Lodash utilities and methods
  • Zero-dependency lightweight function binding

The module is part of Lodash 3.x's modular architecture, allowing selective importing of specific utilities without the full Lodash library.