How to use the bind function from lodash

Find comprehensive JavaScript lodash.bind code examples handpicked from public code repositorys.

lodash.bind creates a new function that, when called, has its this keyword set to the provided value.

6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
 *
 * var func = function(greeting) {
 *   return greeting + ' ' + this.name;
 * };
 *
 * func = _.bind(func, { 'name': 'fred' }, 'hi');
 * func();
 * // => 'hi fred'
 */
function bind(func, thisArg) {
fork icon73
star icon711
watch icon29

30
31
32
33
34
35
36
37
38
39
module.exports.attempt             = _.attempt;
module.exports.before              = _.before;
module.exports.best                = _.best;
module.exports.binPick             = _.binPick;
module.exports.binary              = _.binary;
module.exports.bind                = _.bind;
module.exports.bindAll             = _.bindAll;
module.exports.bindKey             = _.bindKey;
module.exports.bitwiseAnd          = _.bitwiseAnd;
module.exports.bitwiseLeft         = _.bitwiseLeft;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.bind work?

lodash.bind is a function in the Lodash library that returns a new function with a bound this value and partially applied arguments based on the original function. When bind is called, the first argument passed in is the value that this should be bound to when the returned function is called. Any additional arguments passed in are used to partially apply the original function, which means that the values are set and cannot be changed. The returned function can be invoked with additional arguments that will be passed to the partially applied function in addition to the bound arguments. The bind method is useful for creating functions that can be called with a specific context or set of arguments, without needing to explicitly specify them each time the function is called.

3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
  // Returns function property of object by name, bound to object
  _.bound = function(obj, fname) {
    var fn = obj[fname];
    if (!_.isFunction(fn))
      throw new TypeError("Expected property to be a function");
    return _.bind(fn, obj);
  };


})(this);

fork icon3
star icon2
watch icon1

+ 117 other calls in file

299
300
301
302
303
304
305
306
307
308
309
310
311
console.log(ary(1, 2, 3)); // => 1


const before = _.before(2, () => console.log('hello'));
before();


const bind = _.bind(console.log, console, 'hello');
bind(); // => 'hello'


const bindKey = _.bindKey(console, 'log', 'hello');
bindKey(); // => 'hello'
fork icon0
star icon4
watch icon0

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const module = {
  x: 42,
  getX: function () {
    return this.x;
  },
};

const boundGetX = _.bind(module.getX, module);
console.log(boundGetX()); // Output: 42

In this example, lodash.bind is used to bind the function module.getX to the module object so that this inside getX refers to the module object, and not the global object. The resulting function boundGetX is then called to return the value of x from the module object.

29
30
31
32
33
34
35
36
37
38
    console.log('greeting: ', greeting);
    console.log('punctuation: ', punctuation);
    return greeting + ' ' + this.user + punctuation;
}
var object = { 'user': 'fred' };
var bound = _.bind(greet, object, 'hi');
let res1 = bound('!');
console.log('res1--->', res1);
//res1---> hi fred!

fork icon0
star icon0
watch icon0

+ 11 other calls in file

170
171
172
173
174
175
176
177
178
179

  if (len > 3) {
    return `Array[${len}]`
  }

  return `[${_.map(value, _.bind(this.stringifyActual, this)).join(', ')}]`
}

if (_.isRegExp(value)) {
  return value.toString()
fork icon0
star icon0
watch icon0

3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
if (err) return cb(err);

var derivators = [];
_.each([false, true], function(isChange) {
  derivators.push({
    derive: _.bind(wallet.createAddress, wallet, isChange),
    rewind: _.bind(wallet.addressManager.rewindIndex, wallet.addressManager, isChange),
  });
  if (opts.includeCopayerBranches) {
    _.each(wallet.copayers, function(copayer) {
fork icon0
star icon0
watch icon0

+ 7 other calls in file

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)