How to use the bind function from underscore

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

underscore.bind returns a new function with the given this context and partially applied arguments to the original function.

210
211
212
213
214
215
216
var runLogInstance = new RunLog;
_.each(
  ['log', 'logTemporary', 'logRestart', 'logClientRestart', 'logAppOutput',
   'setRawLogs', 'finish', 'clearLog', 'getLog'],
  function (method) {
    exports[method] = _.bind(runLogInstance[method], runLogInstance);
  });
fork icon14
star icon112
watch icon21

+ 3 other calls in file

1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
  var initial = arguments.length > 2;
  if (obj == null) obj = [];
  if (nativeReduce && obj.reduce === nativeReduce) {
    if (context) iterator = _.bind(iterator, context);
    return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
  }
  each(obj, function(value, index, list) {
    if (!initial) {
fork icon0
star icon2
watch icon0

+ 3 other calls in file

How does underscore.bind work?

underscore.bind is a method in the Underscore.js library that returns a new function with the same body and scope as the original function, but with its this value set to the provided this value, and optionally partially applying arguments to the function.

570
571
572
573
574
575
576
577
578
579
  if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
    if (context) iterator = _.bind(iterator, context);
    return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
  }
  var reversed = _.toArray(obj).reverse();
  if (context && !initial) iterator = _.bind(iterator, context);
  return initial ? _.reduce(reversed, iterator, memo, context) : _.reduce(reversed, iterator);
};

// Return the first value which passes a truth test. Aliased as `detect`.
fork icon0
star icon0
watch icon1

+ 27 other calls in file

18
19
20
21
22
23
24
25
26
27
    me.base(options);
    me.data.ol2Helper = null;
    me.data.queryUrl = me.getSfmModuleMgr().getServiceUrl('stQuery');
    me.data.layerManager = options.layerManager;
    me.data.url = me.data.queryUrl + 'identify';
    me.data._bindedOnMapClick = _.bind(me._onMapClick, me);
    me.data.isBusy = false;
    me.data.minr = options && options.minr != undefined ? options.minr : 0.3;
    me.data.rs = options && options.rs != undefined ? options.rs : 2;
},
fork icon0
star icon0
watch icon1

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const myObj = {
  greeting: "Hello, ",
  greet(name) {
    console.log(this.greeting + name);
  },
};

const person = {
  name: "Alice",
};

const boundFn = _.bind(myObj.greet, myObj, person.name);
boundFn(); // logs 'Hello, Alice'

In this example, _.bind is used to create a new function boundFn that is bound to the myObj.greet method. The first argument to _.bind is the function to be bound, the second argument is the value of this inside the function, and any subsequent arguments are arguments that will be passed to the bound function when it is called. In this case, the person.name argument is passed to the bound function, so the final output will be 'Hello, Alice'.

22
23
24
25
26
27
28
29
30
31
    me.base(options);
    me.map = me.doAction('getMap');
    me.ol2Helper = new OL2Helper(me.map);
    me.data.queryUrl = me.getSfmModuleMgr().getServiceUrl('stQuery');
    me.url = me.data.queryUrl + 'identify';
    me._bindedOnMapClick = _.bind(me._onMapClick, me);
    me.isBusy = false;
    me.map.events.register('click', me.map, me._bindedOnMapClick);
},
destroy: function() {
fork icon0
star icon0
watch icon1

+ 2 other calls in file