How to use thisArg in Lo-Dash?

javascript, lodash

Solution

Argument `thisArg` is just a pointer to `this` context of the caller that will also be used as the context of callback. It is provided for convenience.

The result is the same as if using native `Function.bind()` method e.g.:

_.map(collection, callback.bind(this));

An alternative approach would be to make an alias variable of `this` and use it instead.

Problem

How do I use the thisArg in Lo-Dash? Is it possible to combine it with chaining? This is an example that uses thisArg from their documentation: ``` _.map(collection, [callback=identity], [thisArg]) Creates an array of values by running each element in the collection through the callback. The callback is bound to thisArg and invoked with three arguments; (value, index|key, collection). If a property name is provided for callback the created "_.pluck" style callback will return the property value of the given element. If an object is provided for callback the created "_.where" style callback will return true for elements that have the properties of the given object, else false. Aliases _.collect Arguments collection (Array|Object|string): The collection to iterate over. [callback=identity] (Function|Object|string): The function called per iteration. If a property name or object is provided it will be used to create a ".pluck" or ".where" style callback, respectively. [thisArg] (*): The this binding of callback. Returns (Array): Returns a new array of the results of each callback execution. ``` An example would be appreciated, as I wasn't able to find any in their documentation.

Original source