javascript - 如何在 Lo-Dash 中使用 thisArg?

标签 javascript lodash

如何在 Lo-Dash 中使用 thisArg?是否可以将它与链接结合起来?这是一个使用其文档中的 thisArg 的示例:

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

我将不胜感激,因为我无法在他们的文档中找到任何示例。

最佳答案

Argument thisArg 只是指向调用者的 this 上下文的指针,它也将用作回调的上下文。它是为了方便而提供的。

结果与使用 native Function.bind() 方法相同,例如:

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

另一种方法是创建一个 this 的别名变量并使用它来代替。

关于javascript - 如何在 Lo-Dash 中使用 thisArg?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22625147/

相关文章:

underscore.js - 将 mixin() 函数与 lodash 和 JSONSelect 结合使用

javascript - Jquery slider 不适用于 Angular

javascript - React - Javascript 和 JSX 的条件渲染,条件包装组件

typescript - 使用 "first"方法接口(interface)为我的项目扩展 lodash 类型 (@types/lodash)

javascript - 如何使用 lodash 根据值对嵌套数组进行排序?

javascript - 如何获得与lodash的交集?

javascript - 如何从数组中排除某些键

javascript - Response.Redirect(Request.RawUrl) 之后的 Response.Write

javascript - 选项卡内容与其他选项卡内容重叠

javascript - JavaScript WebAudio产生多种频率的声音