or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

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

0

# lodash._bindcallback

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: lodash._bindcallback

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install lodash._bindcallback`

10

11

## Core Imports

12

13

```javascript

14

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

15

```

16

17

## Basic Usage

18

19

```javascript

20

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

21

22

// Basic function binding with context

23

function greet(name) {

24

return this.prefix + ' ' + name;

25

}

26

27

var context = { prefix: 'Hello' };

28

var boundGreet = bindCallback(greet, context);

29

console.log(boundGreet('World')); // "Hello World"

30

31

// Optimized binding with argument count

32

function processItem(value, index, collection) {

33

return this.transform(value) + index;

34

}

35

36

var processor = { transform: function(v) { return v.toUpperCase(); } };

37

var boundProcessor = bindCallback(processItem, processor, 3);

38

console.log(boundProcessor('hello', 0, ['hello'])); // "HELLO0"

39

```

40

41

## Capabilities

42

43

### Callback Binding

44

45

Creates a bound version of a function with specified `this` context and optional argument count optimization.

46

47

```javascript { .api }

48

/**

49

* A specialized version of baseCallback which only supports this binding

50

* and specifying the number of arguments to provide to func.

51

*

52

* @param {Function} func The function to bind.

53

* @param {*} thisArg The this binding of func.

54

* @param {number} [argCount] The number of arguments to provide to func.

55

* @returns {Function} Returns the callback.

56

*/

57

function bindCallback(func, thisArg, argCount);

58

```

59

60

**Parameters:**

61

- `func` (Function): The function to bind. If not a function, returns identity function.

62

- `thisArg` (*): The `this` binding context for the function. If undefined, returns original function.

63

- `argCount` (number, optional): Number of arguments to optimize for. Supports optimized implementations for 1, 3, 4, and 5 arguments.

64

65

**Returns:**

66

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

67

68

**Behavior:**

69

- Returns identity function if `func` is not a function

70

- Returns original function if `thisArg` is undefined

71

- Provides optimized implementations for argument counts 1, 3, 4, and 5

72

- Falls back to general `apply` pattern for other argument counts

73

74

**Usage Examples:**

75

76

```javascript

77

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

78

79

// Example 1: Basic binding without argument count

80

function multiply(a, b) {

81

return this.factor * a * b;

82

}

83

var math = { factor: 2 };

84

var boundMultiply = bindCallback(multiply, math);

85

console.log(boundMultiply(3, 4)); // 24

86

87

// Example 2: Optimized for single argument (argCount: 1)

88

function transform(value) {

89

return this.prefix + value + this.suffix;

90

}

91

var formatter = { prefix: '[', suffix: ']' };

92

var boundTransform = bindCallback(transform, formatter, 1);

93

console.log(boundTransform('test')); // "[test]"

94

95

// Example 3: Optimized for collection iteration (argCount: 3)

96

function processElement(value, index, collection) {

97

return this.base + value + index;

98

}

99

var processor = { base: 'item-' };

100

var boundProcess = bindCallback(processElement, processor, 3);

101

// Optimized for array map/forEach operations

102

103

// Example 4: Optimized for reduce operations (argCount: 4)

104

function accumulate(acc, value, index, collection) {

105

return acc + this.weight * value;

106

}

107

var reducer = { weight: 0.5 };

108

var boundAccumulate = bindCallback(accumulate, reducer, 4);

109

// Optimized for array reduce operations

110

111

// Example 5: Edge case - non-function input

112

var notAFunction = "not a function";

113

var result = bindCallback(notAFunction, {}); // Returns identity function

114

115

// Example 6: Edge case - undefined thisArg

116

var originalFunc = function() { return 'test'; };

117

var unchanged = bindCallback(originalFunc, undefined); // Returns originalFunc

118

```

119

120

## Performance Optimizations

121

122

The function provides specialized implementations for common argument patterns:

123

124

- **1 argument**: Optimized for simple transformations and filters

125

- **3 arguments**: Optimized for array iteration methods (map, forEach) with `(value, index, collection)`

126

- **4 arguments**: Optimized for reduce operations with `(accumulator, value, index, collection)`

127

- **5 arguments**: Optimized for comparison operations with `(value, other, key, object, source)`

128

129

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

130

131

## Error Handling

132

133

- **Invalid function input**: Returns identity function instead of throwing

134

- **Undefined context**: Returns original function unchanged

135

- **Missing arguments**: Handles gracefully with default behavior

136

137

## Implementation Notes

138

139

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

140

- High-performance callback binding scenarios

141

- Functional programming patterns requiring context binding

142

- Integration with other Lodash utilities and methods

143

- Zero-dependency lightweight function binding

144

145

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