javascript 请说明underscore.js的_.identity(value)的用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14216479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Please explain usage of _.identity(value) of underscore.js
提问by SunnyShah
Please explain usage of _.identity(value)
of underscore.js. Not able to understand it from the documentation ( http://underscorejs.org/#identity).
请解释_.identity(value)
underscore.js 的用法。无法从文档(http://underscorejs.org/#identity)中理解它。
Can you provide some example of its usage?
你能提供一些它的用法的例子吗?
采纳答案by Matt Sach
It's essentially a no-operation function. It returns the value of whatever was passed into it.
它本质上是一个无操作功能。它返回传入它的任何值。
The part about it being used as a "default iterator" within the library itself means that in other functions which may have an optional "iterator" parameter (which is likely used as a function to apply to each element of an array of some kind), if no iterator parameter is passed, the library will use this "no-op" iterator instead and the elements of the array will remain unchanged.
关于它在库本身中用作“默认迭代器”的部分意味着在其他可能具有可选“迭代器”参数的函数中(它可能用作应用于某种数组的每个元素的函数) , 如果没有传递迭代器参数,库将使用这个“无操作”迭代器,并且数组的元素将保持不变。
回答by Robert Monfera
A JavaScript code pattern involving identity is filtering values based on truthiness, e.g.
涉及身份的 JavaScript 代码模式是基于真实性过滤值,例如
var a = [null, null, [1,2,3], null, [10, 12], null];
a.filter(_.identity)
yields [Array[3], Array[2]].
产生 [Array[3], Array[2]]。
Using
使用
_.compact(a)
is clearer, but one may not use lodash or underscore at all, e.g.
更清楚,但可能根本不使用 lodash 或下划线,例如
function identity(x) {
return x;
}
a.filter(identity)
Whether it is a good code pattern is questionable for several reasons, but it's being used in the wild.
由于多种原因,它是否是一个好的代码模式值得怀疑,但它正在被广泛使用。
It is not a NOOP at all. A NOOP is an imperative construct in e.g. assembly, whereas in functional programming, it is like other functions in that it returns a value. If identity were a NOOP, then all pure functions could also be considered noop, and it would not be a sensible thing.
它根本不是 NOOP。NOOP 在例如汇编中是一种命令式构造,而在函数式编程中,它与其他函数一样,因为它返回一个值。如果identity是一个NOOP,那么所有的纯函数也都可以被认为是noop,这不是明智之举。
回答by Teruki Shinohara
A specific example:
一个具体的例子:
Underscore.js defines _.each and as like this.
Underscore.js 定义 _.each 和这样。
_.each = function(obj, iterator, context) {
...
}
This iterator shows el value. You maybe have used this idiom.
此迭代器显示 el 值。你可能用过这个习语。
_.each([1, 2, 3], function(el){
console.log(el);
});
This iterator returns el value without change.
此迭代器返回 el 值而没有更改。
_.each([1, 2, 3], function(el){
return el;
});
The function that returns a value without change occur frequently. So Underscore.js wants to define the function. Underscore.js names the function _.identity.
返回值不变的函数经常出现。所以 Underscore.js 想要定义这个函数。Underscore.js 将函数命名为 _.identity。
_.identity = function(value) {
return value;
};
If Underscore.js wants to use a default iterator, all Underscore.js need is call _.identity.
如果 Underscore.js 想要使用默认迭代器,Underscore.js 只需要调用 _.identity。
_.each([1, 2, 3], _.identity);