lodash _.identity
例子
此方法仅返回第一个参数。
var res1 = _.identity(10, 20); // res1现在是10 var res2 = _.identity("hello", "world"); // res2 now is "hello"
在lodash文档中_.identity是什么意思?
此方法在lodash文档中使用,而不是function(x){returnx;}(或等效于ES6x=>x)使用。
它通常表示“无转换”或用作谓语:值的真实性。
的实施例_.identity中的文档_.times
该_.times函数有两个参数。它在文档中这样表示:varres=_.times(n,[iteratee=_。identity])
该iteratee作为他们遍历变换的值。
文档显示iteratee参数是可选的,如果省略,它将具有默认值_.identity,在这种情况下表示“无转换”
var res = _.times(5); // 返回[0,1,2,3,4] // 含义与: var res = _.times(5, _.identity); // 再次与以下内容相同: var res = _.times(5, function(x){ return x; }); // 或使用ES6箭头语法: var res = _.times(5, x => x);
的实施例_.identity中的文档_.findKey和_.findLastKey
该_.findKey和_.findLastKey功能需要两个参数。它在文档中这样表示:_.findKey(object,[predicate=_。identity])和_.findLastKey(object,[predicate=_。identity])
这再次表示第二个参数是可选的,如果省略,它将具有默认值_.identity,在这种情况下,这意味着“任何真实的东西”的第一个(或最后一个)
var p = { name: "Peter Pan", age: "Not yet a grownup", location: "Neverland" }; var res1 = _.findKey(p); // returns "name" var res2 = _.findLastKey(p); // returns "location" // 含义与: var res1 = _.findKey(p, _.identity); var res2 = _.findLastKey(p, _.identity); // 这又意味着: var res1 = _.findKey(p, function(x) { return x; } var res2 = _.findLastKey(p, function(x) { return x; } // 或使用ES6箭头语法: var res1 = _.findKey(p, x => x); var res2 = _.findKey(p, x => x);