or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

tessl/npm-lodash--bind

The lodash method `_.bind` exported as a module.

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

To install, run

npx @tessl/cli install tessl/npm-lodash--bind@4.2.0

0

# Lodash Bind

1

2

The 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.

3

4

## Package Information

5

6

- **Package Name**: lodash.bind

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

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

10

11

## Core Imports

12

13

```javascript

14

const bind = require('lodash.bind');

15

```

16

17

For ES modules:

18

19

```javascript

20

import bind from 'lodash.bind';

21

```

22

23

## Basic Usage

24

25

```javascript

26

const bind = require('lodash.bind');

27

28

function greet(greeting, punctuation) {

29

return greeting + ' ' + this.user + punctuation;

30

}

31

32

const object = { user: 'fred' };

33

34

// Simple binding

35

const bound = bind(greet, object, 'hi');

36

bound('!');

37

// => 'hi fred!'

38

39

// With placeholder arguments

40

const bound2 = bind(greet, object, bind.placeholder, '!');

41

bound2('hello');

42

// => 'hello fred!'

43

```

44

45

## Capabilities

46

47

### Function Binding

48

49

Creates a function that invokes the original function with a specific `this` context and optionally pre-applied arguments.

50

51

```javascript { .api }

52

/**

53

* Creates a function that invokes `func` with the `this` binding of `thisArg`

54

* and prepends any additional `bind` arguments to those provided to the

55

* bound function.

56

*

57

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

58

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

59

* @param {...*} [partials] The arguments to be partially applied.

60

* @returns {Function} Returns the new bound function.

61

*/

62

function bind(func, thisArg, ...partials);

63

```

64

65

**Usage Examples:**

66

67

```javascript

68

const bind = require('lodash.bind');

69

70

// Basic binding

71

function sayHello() {

72

return 'Hello, ' + this.name;

73

}

74

75

const person = { name: 'Alice' };

76

const boundSayHello = bind(sayHello, person);

77

boundSayHello(); // => 'Hello, Alice'

78

79

// Partial application

80

function add(a, b, c) {

81

return a + b + c;

82

}

83

84

const addTwo = bind(add, null, 2);

85

addTwo(3, 4); // => 9 (2 + 3 + 4)

86

87

// Placeholder usage

88

const addToFive = bind(add, null, bind.placeholder, 5);

89

addToFive(3, 2); // => 10 (3 + 5 + 2)

90

```

91

92

### Placeholder Support

93

94

The bind function supports placeholder arguments using `bind.placeholder`.

95

96

```javascript { .api }

97

/**

98

* The placeholder value used for partially applied arguments.

99

* Defaults to the bind function itself.

100

*/

101

bind.placeholder;

102

```

103

104

**Usage Examples:**

105

106

```javascript

107

const bind = require('lodash.bind');

108

109

function multiply(a, b, c) {

110

return a * b * c;

111

}

112

113

// Skip the first argument using placeholder

114

const multiplyBy2And3 = bind(multiply, null, bind.placeholder, 2, 3);

115

multiplyBy2And3(4); // => 24 (4 * 2 * 3)

116

117

// Multiple placeholders

118

const customMultiply = bind(multiply, null, bind.placeholder, bind.placeholder, 5);

119

customMultiply(2, 3); // => 30 (2 * 3 * 5)

120

```

121

122

## Key Features

123

124

- **Enhanced Function Binding**: More powerful than native `Function.prototype.bind`

125

- **Partial Application**: Pre-apply arguments to create specialized functions

126

- **Placeholder Support**: Skip arguments using placeholders for flexible argument patterns

127

- **Consistent Behavior**: Reliable cross-browser compatibility

128

- **No Length Property**: Unlike native bind, doesn't set the "length" property of bound functions

129

130

## Error Handling

131

132

The bind function will throw a `TypeError` if the first argument is not a function:

133

134

```javascript

135

const bind = require('lodash.bind');

136

137

try {

138

bind('not a function', {});

139

} catch (error) {

140

console.log(error.message); // => "Expected a function"

141

}

142

```

143

144

## Differences from Native bind

145

146

- **Placeholder Support**: Native `bind` doesn't support placeholders

147

- **Length Property**: Native `bind` sets the `length` property, lodash bind doesn't

148

- **Performance**: Lodash bind may have different performance characteristics

149

- **Cross-browser Consistency**: Lodash bind provides consistent behavior across environments

150

151

## Types

152

153

```javascript { .api }

154

/**

155

* The bind function type definition

156

*/

157

interface BindFunction {

158

<T extends Function>(func: T, thisArg: any, ...partials: any[]): T;

159

placeholder: any;

160

}

161

```