我无法理解underscore js方法
两者似乎都会在每个项目上调用传递的函数。
在什么情况下应该使用
请提供一些示例以说明区别。
_.each
和_.invoke
之间的区别。两者似乎都会在每个项目上调用传递的函数。
在什么情况下应该使用
_.each
和_.invoke
?请提供一些示例以说明区别。
_.each
和_.invoke
之间的区别。_.each
和_.invoke
?each
calls a given function with each element of a given object. You can additionally pass it a context on which the functions are applied. It acts like the native forEach
on arrays.
iterator.call(context, obj[i], i, obj)
It does return undefined.
invoke
usually gets a method name as a string, and looks up the method dynamically for each element of the given set. Then it applies the method on that element; and you additionally can pass it some arguments.
(_.isFunction(method) ? method : obj[i][method]).apply(obj[i], args);
It does return the results of the invocations, it basically does map
.